给出特定产品或项目的条件总和

Give a Conditional SUM on spesifict product or Item

我有一个table这样的

Product Quantity
a          2
b          3
c          4
d          1
e          5      

我想知道如何对特定产品(如 c、d 和 e)求和。
我已经尝试过了

SELECT sum(quantity)
  FROM product
 where product.product in ('a', 'b')
 group by product
UNION
select sum(quantity)
  from product
 where product.product IN ('c', 'd', 'e')

但是我无法显示商品名称,最后的结果应该是这样的

Product     Quantity
a              2
b              3
sum(c,d,e)   (4+1+5)

您可以使用 CASE 语句对特定项目进行分组。如果需要,您可以添加多个组。你不需要子查询。

select CASE WHEN ProductName IN ('c','d','e') THEN 'bde' ELSE  ProductName END,
SUM(Quentity) from product
group by CASE WHEN ProductName IN ('c','d','e') THEN 'bde' ELSE  ProductName END
;