Saturday, November 30, 2013

sql SUM question



I have a table named Trans, and that is the data contained.



Customer Product Ordered
C1 a 2
C2 b 1
C3 a 1
C1 a 2









I wonder a ordered total summary about product 'a' for every single customer, and I use this query,



SELECT
Trans.Customer,
SUM(Trans.Ordered) AS OrderedTotal
FROM Trans
WHERE Product = a
GROUP BY Trans.Customer




So I get this



Customer OrderedTotal
C1 4
C3 1




But the query above can't return the result like this, so is it possible to write a query that let customer 'C2' in the final result?


Customer OrderedTotal
C1 4
C2 0
C3 1

Thanks, everyone.


No comments:

Post a Comment