I have a SQL with list of Customer IDs and invoices, the specific product purchased in each invoice, and the income of each invoice. I need to write a query that will result in a list of customer IDs in one column, and the sum of total income from purchasing “product A”, by each customer, in another column.
How do I do that?
Advertisement
Answer
One method is conditional aggregation:
select customerid, sum(case when productid = 'product A' then income else 0 end) as income_a from a group by customerid;