Compare columns from SUM(CASE...END) in the same query - mysql

I'm trying to add a column named Ready to my query that checks if the sum of Complete is = to the sum of Total and places 'Y' if True and 'N' if FALSE.
Here is my query that works producing 4 columns
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS 'Complete',
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS 'Not Started',
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS 'Total'
FROM `pc`
Group By pc.Sub WITH ROLLUP
I just cant figure out how to create the extra column if it is possible at all.
Regards

Try outer SELECT
SELECT `Complete`,
`Not Started`,
`Total`,
CASE WHEN `Complete` = `Total` THEN 'Y' ELSE 'N' END `Ready`
FROM (
SELECT pc.Sub,
SUM(CASE WHEN `SheetStatus` LIKE 'Complete' THEN 1 ELSE 0 END) AS `Complete`,
SUM(CASE WHEN `SheetStatus` LIKE 'Not started' THEN 1 ELSE 0 END) AS `Not Started`,
SUM(CASE WHEN `CheckSheet` LIKE '%' THEN 1 ELSE 0 END) AS `Total`
FROM `pc`
GROUP BY pc.Sub WITH ROLLUP) t

Try this:
SELECT pc.Sub,
SUM(IF(`SheetStatus`='Complete',1,0) AS 'Complete',
SUM(IF(`SheetStatus`='Not started',1,0) AS 'Not Started',
SUM(IF(`CheckSheet` LIKE '%',1,0) AS 'Total'
IF(SUM(IF(`CheckSheet` LIKE '%',1,0) = SUM(IF(`SheetStatus`='Complete',1,0),"Y","N") AS Ready
FROM `pc`
Group By pc.Sub WITH ROLLUP

Related

Count each day with SQL query

I want to display a table with SQL query like this:
I have run my query:
select
tb_r_orderdata.finishtime as date ,
count(*)sum all,
sum(when status = 'SUCCESS' and issync = '1' then 1 else 0 end) sumpaid,
sum(when status = 'SUCCESS' and issync in ('3', '4') then 1 else 0 end) sumfail,
sum(when status = 'CLOSED' then 1 else 0 end) sumclose,
sum(when status = 'Null' then 1 else 0 end) sumunflag
from
tb_r_orderdata;
But when I execute it, the result is different than what I expected. The result is like this:
Thank you for any help
You are missing the GROUP BY and the CASE:
select tb_r_orderdata.finishtime as date ,
COUNT(*) as sumall,
SUM(CASE WHEN status='SUCCESS' AND issync='1' then 1 ELSE 0 END) as sumpaid,
SUM(CASE WHEN status='SUCCESS' AND issync in ('3','4') then 1 ELSE 0 END) as sumfail,
SUM(CASE WHEN status='CLOSED' then 1 ELSE 0 END) as sumclose,
SUM(CASE WHEN status is null then 1 ELSE 0 END) as sumunflag
from tb_r_orderdata
group by tb_r_orderdata.finishtime ;
MySQL treats booleans as integers in a numeric context, with "1" for true and "0" for false. You can simplify your query to:
select o.finishtime as date ,
COUNT(*) as sumall,
SUM(status = 'SUCCESS' AND issync = '1') as sumpaid,
SUM(status = 'SUCCESS' AND issync in ('3', '4')) as sumfail,
SUM(status = 'CLOSED') as sumclose,
SUM(status is null) as sumunflag
from tb_r_orderdata o
where tb_r_orderdata.finishtime is not NULL
group by o.finishtime ;
This also removes NULL finish times.
Explanation in comment for Gordon Linoff
i have try your answer but there is record NULL in the top of table, i dont know why, can you explain it
with little bit change from Gordon Linoff answer
select tb_r_orderdata.finishtime as date ,
SUM(CASE WHEN status='SUCCESS' AND issync='1' then 1 ELSE 0 END) as sumpaid,
SUM(CASE WHEN status='SUCCESS' AND issync in ('3','4') then 1 ELSE 0 END) as sumfail,
SUM(CASE WHEN status='CLOSED' then 1 ELSE 0 END) as sumclose,
SUM(CASE WHEN status='NULL' then 1 ELSE 0 END) as sumunflag
from tb_r_orderdata
group by tb_r_orderdata.finishtime

MySQL CASE , SUM, GROUP BY

I want sum of payment status from two differnt columns based on paymentstatus value - but this query returns null for sum. Why is it not working?
select payment_status,
CASE
WHEN 'PAID' THEN sum(paid_amount)
when 'Not Paid' then sum(total_amount_due )
END
from monthly_fee
group by payment_status;
select sum(CASE WHEN payment_status = 'PAID' THEN paid_amount else 0 end) as paid,
sum(CASE WHEN payment_status = 'Not Paid' THEN total_amount_due else 0 end) as due
from monthly_fee
If you want this conditionally, you need to include the column in the case:
select payment_status,
(case payment_status
when 'Paid' then sum(paid_amount)
when 'Not Paid' then sum(total_amount_due )
end)
from monthly_fee
group by payment_status;
This seems like a strange way to write the query, unless you really want two rows.
Your WHEN clauses aren't a condition.
I'd expect to see something like
select payment_status,
CASE
WHEN payment_status = 'PAID' THEN sum(paid_amount)
when payment_status = 'Not Paid' then sum(total_amount_due )
END
from monthly_fee
group by payment_status;
You can try the following query:
select sum(if(payment_status = 'PAID', paid_amount, 0)
+ if(payment_status = 'Not Paid', total_amount_due, 0))
from monthly_fee
group by payment_status;

how to use regular expression in MySQL where like and in statement in where clause

I have a query where I have to look in 3 columns from a table where I have 2 columns searched on message column and last column should be on like . how can I use both statements at a time.
SELECT date(datetime) as dateonly ,
sum(CASE message when 'Accepted Images' then 1 else 0 end) as AcceptedIMG,
sum(CASE message when 'Rejected Images' then 1 else 0 end) as RejectedIMG,
sum(CASE when message like '%Changed to %' then 1 else 0 end) as ChangeIMG
FROM customer_1.audit_trail
WHERE message like ('%Changed to %') and message in ('Accepted Images','Rejected Images')
GROUP BY (dateonly)
ORDER BY (dateonly) asc ;
You can use OR (and group your condition)
SELECT date(datetime) as dateonly ,
sum(CASE message when 'Accepted Images' then 1 else 0 end) as AcceptedIMG,
sum(CASE message when 'Rejected Images' then 1 else 0 end) as RejectedIMG,
sum(CASE when message like '%Changed to %' then 1 else 0 end) as ChangeIMG
FROM customer_1.audit_trail
WHERE (message like ('%Changed to %') OR message in ('Accepted Images','Rejected Images'))
GROUP BY (dateonly)
ORDER BY (dateonly) asc ;

Invalid use of group function (For using count in select)

I got an error output on my sql query with remarks " Invalid use of group function "
This is my query
SELECT * FROM
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=6 THEN 1 ELSE NULL END) as `week 6`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=2 THEN 1 ELSE NULL END) as `week 2`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=3 THEN 1 ELSE NULL END) as `week 3`,
COUNT(CASE WHEN WEEK(MAX(plannedGI))+1=4 THEN 1 ELSE NULL END) as `week 4`
FROM tbl_nif,tbl_custmon,tbl_incoming
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment
AND plannedGI!='0000-00-00'
GROUP BY tbl_custmon.so
ORDER BY tbl_custmon.so) AS grid
ORDER BY project ASC
If I not include the "count" in query, it will be show the output.
So, kindly please tell me an idea for this issue.
Big Thanks!
CONCLUSION
Put "Max" function on query makes a natural "group by".
So I put "Max" function inline with the other SELECT field.
So the query will be like this
SELECT * FROM
(SELECT project,tbl_nif.POno,tbl_custmon.so, tbl_nif.matcode, tbl_nif.prodesc, MAX(plannedGI),
COUNT(CASE WHEN WEEK(plannedGI)+1=6 THEN 1 ELSE NULL END) as `week 6`,
COUNT(CASE WHEN WEEK(plannedGI)+1=2 THEN 1 ELSE NULL END) as `week 2`,
COUNT(CASE WHEN WEEK(plannedGI)+1=3 THEN 1 ELSE NULL END) as `week 3`,
COUNT(CASE WHEN WEEK(plannedGI)+1=4 THEN 1 ELSE NULL END) as `week 4`
FROM tbl_nif,tbl_custmon,tbl_incoming
WHERE tbl_nif.so=tbl_custmon.so AND tbl_nif.bill='521513' AND tbl_incoming.shipment!=tbl_custmon.shipment
AND plannedGI!='0000-00-00'
GROUP BY tbl_custmon.so
ORDER BY tbl_custmon.so) AS grid
ORDER BY project ASC
And the output will show data that I need.
Many thanks to you :)

Case When with AND?

I want to use Case When with AND condition and it is not calculating the sum properly.
For example:
SELECT DATE(`SubmitDate`),
SUM(CASE status WHEN 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
SUM(CASE status WHEN 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
You need to use CASE WHEN [Condition] THEN... rather than a simple case expression:
SELECT DATE(`SubmitDate`),
SUM(CASE WHEN status = 'New' AND `Type` = 'consumer' THEN 1 ELSE 0 END) as new_consumer,
SUM(CASE WHEN status = 'New' AND `Type` = 'business' THEN 1 ELSE 0 END) as new_business
FROM report
WHERE `source` = 'net'
group by DATE(`SubmitDate`) Order by `SubmitDate` DESC
You should write
CASE WHEN status='New' AND `Type` = 'consumer' THEN 1 ELSE 0 END
Check the syntax of CASE WHEN