GuyTal
GuyTal

Reputation: 207

SQL - Sum of specific column grouped by a specific value of another column

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?

Upvotes: 0

Views: 47

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270371

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;

Upvotes: 2

Related Questions