I'm trying sum up the total_grand column of my orders table.
So I have this query
SELECT sum(total_grand) as total
FROM `orders`
WHERE overall_status in ("In-Transit","Not Yet Shipped","Not Yet Validated");
It works fine. But I need to add new conditional statement.
Here's my sample orders table columns with data.
total_grand
overall_status
televalidator_user_id
100
In-Transit
1
200
Not Yet Shipped
1
300
Not Yet Validated
NULL
400
Not Yet Validated
1
500
In-Transit
1
---------------------
------------------
------------------
I'm trying to sum up the total_grand but if televalidator_user_id is NULL, It should not be added to the sump
Since there is null televalidator_user_id on row 3
the output must be: 1200
SELECT sum(total_grand) as total
FROM `orders`
WHERE overall_status in ("In-Transit", "Not Yet Shipped", "Not Yet Validated")
AND televalidator_user_id is not null;
Related
I have a table like this:
REFERENCE QUANTITY
AS400 0
AS400 30000
AB123 500
CA031 560
CA031 25
I need to have, in sql, the total quantity if the rows are the same.
For ex:
AS400 30000
AB123 500
CA031 585
You could using group by like below query
select REFERENCE, sum(QUANTITY)
from tableA
group by REFERENCE
If do you want to get the quantites by reference from ps_product_attribute, the code which is working on Ps1.7.6 :
select `reference`, sum(`quantity`)
from `ps_product_attribute`
group by `reference`
I am trying to achieve a SQL statement where I can get the number of installments from multiple same installments and also get 1 duedate from all duedates and sum the rest rows up.
Here is my Table in a picture.
What I want to achieve is this.
it will return me rows like
instalmentnbr | duedate | capitalpayment | interest_payment
1 2017-04-13 sum(capitalpayment) sum(interest_payment )
2 2017-05-13 sum(capitalpayment) sum(interest_payment )
3 2017-06-12 sum(capitalpayment) sum(capitalpayment)
So basically getting the 3 installments with their duedate and suming up the rest.
Here is my code.
select a.instalmentnbr, a.duedate, sum(a.capital_payment), sum(a.interest_payment), sum(a.overdue_payment)
from helltable a
where a.request_orig_id = 46 order by a.instalmentnbr;
I was checking out this example but I really dint get how to it works.
How to return only 1 row if multiple duplicate rows and still return rows that are not duplicates?
Sounds like you need to use GROUP BY:
select a.instalmentnbr, a.duedate, sum(a.capital_payment), sum(a.interest_payment), sum(a.overdue_payment)
from helltable a
where a.request_orig_id = 46
group by a.instalmentnbr, a.duedate
order by a.instalmentnbr;
I have a many to many table which has repeating values of student_id, gradelevel_id and schoolyear_id. So my goal is to display records of the most recent schoolyear and last gradelevel taken. In other words, the last grade_level taken by student.
schoolyear_student_lt (MANY TO MANY table)
gradelevel_mt
schoolyear_mt
So is should display 1 unique student_id, 1 grade_level, 1 schoolyear per row on the result set.
I created a stored procedure.
SELECT sslt.gradelevel_id,sy.schoolyear_id, sslt.student_id,MAX(gl.grade_level)
FROM gradelevel_mt gl
INNER JOIN schoolyear_student_lt sslt ON gl.gradelevel_id = sslt.gradelevel_id
INNER JOIN schoolyear_mt sy ON sslt.schoolyear_id = sy.schoolyear_id
WHERE gl.gradelevel_id = sslt.gradelevel_id
GROUP BY sslt.student_id;
But problem is, my stored procedure returns incorrect schoolyear_id and gradelevel_id. Only the student_id and MAX(gl.gradelevel) are correct.
The correct expected result set should be
gradelevel_id | schoolyear_id | student_id | MAX(gl.gradelevel) |
302 401 20170008 1
304 404 20170009 3
For student_id: 20170008 it should be gradelevel_id: 302 (Grade 1) and schoolyear_id : 401 (2016-2017) not 402(2015-2016).
For student_id: 20170009 it should be gradelevel_id: 304 (Grade 3) and schoolyear_id : 404 (2019-2020) not 402(2015-2016).
I hope my explanation is clear. I'd appreciate any help.
Thank you.
I have a problem with a MySQL query, the problem is I have the following table:
id, rep, val dates
1 rep1 200 06/01/2014
2 rep2 300 06/01/2014
3 rep3 400 06/01/2014
4 rep4 500 06/01/2014
5 rep5 100 06/01/2014
6 rep1 200 02/06/2014
7 rep2 300 02/06/2014
8 rep3 900 02/06/2014
9 rep4 700 02/06/2014
10 rep5 600 02/06/2014
and I want a result like this:
rep 01/06/2014 02/06/2014
rep1 200 200
rep2 300 300
rep3 400 900
rep4 500 700
rep5 100 600
thank you very much!
You seem to want the most recent row for each rep. Here is an approach that often performs well:
select t.*
from table t
where not exists (select 1
from table t2
where t2.repid = t.repid and
t2.id > t.id
);
This transforms the problem to: "Get me the rows in table t where there is no other row with the same repid and a larger id." That is the same logic as getting the last one, just convoluted a bit to help the database know what to do.
For performance reasons, an index on t(repid, id) is helpful.
You seem to want the val for each of the dates.
Assuming the dates you are interested in are fixed then you can do that as follows. For output date column you check of the row matches the date for that column. If so you use the value of val , if not you just use 0. Then you sum all the resulting values, grouping by rep. I have assumed a fixed format of date.
SELECT rep, SUM(IF(dates='2014/06/01'), val, 0) AS '2014/06/01', SUM(IF(dates='2014/06/02'), val, 0) AS '2014/06/02'
FROM sometable
GROUP BY rep
Or if you just wanted the highest val for each day
SELECT rep, MAX(IF(dates='2014/06/01'), val, 0) AS '2014/06/01', MAX(IF(dates='2014/06/02'), val, 0) AS '2014/06/02'
FROM sometable
GROUP BY rep
If the number of dates is variable then not really a direct way to do it (as the number of resulting columns would vary). It would be easiest to do this manly in your calling script based on the following, giving you one row per rep / possible date with a sum of the values of val for that rep / date combination:-
SELECT rep, sub0.dates, SUM(IF(sometable.dates=sub0.dates), val, 0)
FROM sometable
CROSS JOIN
(
SELECT DISTINCT dates
FROM sometable
) sub0
GROUP BY rep, sub0.dates
I try to get the max value of a mysql select, but want to have it null/empty/0 if there is one row containing no timestamp.
Table stats (simplyfied):
ID CLIENT ORDER_DATE CANCEL_DATE
1 5 1213567200
2 5 1213567200
3 6 1210629600 1281736799
4 6 1210629600 1281736799
5 7 1201042800 1248386399
6 7 1201042800
7 8 1205449200 1271282399
I'm now looking to get the lowest order date (no problem, as it is never empty), and
the maximum cancel date. If the client has already cancelled his subscription, the cancel date is filled, but if he is still active, there is no cancel date at all.
Query:
SELECT ID, min(ORDER_DATE) AS OD, max(CANCEL_DATE) AS CD FROM stats GROUP BY CLIENT
Returns:
ID OD CD
5 1213567200 // fine
6 1210629600 1281736799 // fine
7 1201042800 1248386399 // Should be empty
8 1205449200 1271282399 // fine
I can't figure it out how to return empty/0/NULL if there is one (or more) empty colums for a client. Also tried with NULL fields.
Thanks for any hint.
I don't know how fast it will be but I guess it can be solved like this:
SELECT ID, min(ORDER_DATE) AS OD,
IF(COUNT(*)=COUNT(CANCEL_DATE),max(CANCEL_DATE),NULL) AS CD
FROM stats GROUP BY CLIENT
I couldn't test it but the idea behind this solution is that count(cancel_date) should count all not null value entries and if it's equal to count(*) that means that there are no null values and it will return max(cancel_date), otherwise null.
You could use a query like this:
SELECT
client,
min(ORDER_DATE) AS OD,
case when MAX(CANCEL_DATE IS NULL)=0 THEN max(CANCEL_DATE) END AS CD
FROM
stats
GROUP BY
CLIENT
Please see fiddle here.
CANCEL_DATE IS NULL will be evaluated either to 0, when CANCEL_DATE is not null, or to 1 when it is null
MAX(CANCEL_DATE IS NULL) will be evaluated to 0 if there are no cancel_date with null values, otherwise its value will be 1.
when MAX(CANCEL_DATE IS NULL)=0 it means that there are no rows where CANCEL_DATE is null, and we need to return MAX(cancel_date) in that case, otherwise we need to return NULL.