Sum quantity only when certain products

I have a SQL that select orders based on specific products, however when I add a SUM column to measure the quantity it also gives me the count of products that are in the order that I don´t need. For example:

ORDER | PRODUCT | QTD
 200      A        1
 200      B        1
 200      C        1

So I only want to sum the product A.

Is it possible to make an SQL like this ?

SELECT SUM(QTD) WHEN PRODUCT LIKE A

Appreciate any help on this matter

Upvotes: 0

Views: 374

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50173

Add case expression inside the sum() function

SELECT SUM(CASE WHEN PRODUCT = 'A' THEN QTD ELSE 0 END) 
. . .

However, this would produce result set with all PRODUCTs but it will do the sum only when PRODUCT = 'A' other will be 0.

Upvotes: 1

Related Questions