Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mysql Query sum and group by not showing a row with null values

I have this query:

SELECT Customer, SUM(SoldUnits) AS SoldUnits
FROM Uploads
WHERE Year = 2021
AND Week = 11
GROUP BY Customer;

And Returns me:

Customer SoldUnits
CUSTOMER A 55
CUSTOMER B 32
CUSTOMER D 17

CUSTOMER C exist, but it doesn’t have data for the week 11 and I want to show CUSTOMER C with 0 SoldUnits. How Can I do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming your table does have all customers you want to appear in the report, you could do a conditional summation and remove the WHERE clause:

SELECT Customer,
       SUM(CASE WHEN Year = 2021 AND Week = 11
                THEN SoldUnits ELSE 0 END) AS SoldUnits
FROM Uploads
GROUP BY Customer;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading