这个输出的正确sql查询是什么?

时间:2015-04-15 08:14:07

标签: sql-server

表1

id    name    purchase   Group            
001   vince   26         G1                  
002   vince   28         G1                  
002   vince   31         G1                  
002   vince   38         G2
003   karl    24         G2
003   karl    24         G2

预期产出

id    name     Purchase
001   vince    26
002   vince    59
003   karl     0

1 个答案:

答案 0 :(得分:0)

问题不是很明确,但这是一个应该有效的查询:

 SELECT id, name, SUM(CASE WHEN [Group] = 'G1' THEN purchase ELSE 0 END) AS Purchase
 FROM Table1
 GROUP BY id, name
 ORDER BY id