基于ID和groupID观察的聚合数据

时间:2013-06-07 15:15:59

标签: sql-server-2008

嘿,我有以下数据:

id       cashpayment     total payment   groupID
1        30.00              100.00           A
1        30.00              100.00           B
1        30.00              100.00           C
1        40.00              100.00           B
2        25.00              400.00           C
2        20.00              300.00           C
2        35.00              200.00           B
3        40.00              200.00           B

我想汇总cahspayment和总付款,以便结果如下所示:

id       cashpayment     total payment   
1        130.00             400.00           
2        80.00              900.00          
3        40.00               200.00      

另外,我想要同样的事情,但考虑到groupID所以我会得到以下

id       cashpayment     total payment   groupID
1        30.00              100.00           A
1        70.00              200.00           B
1        30.00              100.00           C
2        45.00              700.00           C
2        35.00              200.00           B
3        40.00              200.00           B

在这两种情况下,我都需要将其放入新表中

1 个答案:

答案 0 :(得分:1)

select id, sum(cashpayment), sum([total payment])
from table 
group by id
order by ID

表示第一个和

   select id, sum(cashpayment), sum([total payment]), groupID
    from table 
    group by id, groupID
    order by id,groupid

代表第二个