Home > front end >  Mysql Query sum and group by not showing a row with null values
Mysql Query sum and group by not showing a row with null values

Time:04-13

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?

CodePudding user response:

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;
  • Related