SQL Count / sum多列

时间:2016-04-21 18:44:17

标签: sql sql-server

我想在单个查询样本数据中使用count / sum多个字段,所需的结果如下所示:

MemID  claimNum  ItemID PaidAmt 
123    1234      4      5
123    2309      4      5 
123    1209      4      5 
123    1209      8      2.2
123    1210      8      2.2

期望的结果

MemID  count(claimNum) count(ItemID) sum(PaidAmt) 
123    3               3             15 
123    2               2             4.4   

2 个答案:

答案 0 :(得分:1)

看起来您想要按MemID和ItemID进行分组:

select MemID, count(claimNum), count(ItemID), sum(PaidAmt) 
  from the_table 
group by MemID, ItemID

答案 1 :(得分:0)

按ItemID使用分组

select MemID, count(claimNum), count(ItemID), sum(PaidAmt) 
from my_table 
group by MemID, ItemID