SQL 数据分组累加sum() over (partition by ... order by ...)


drop table testaa
--新建测试表testaa
create table testaa (id int ,[group] varchar(10) ,num int )

select  *  from testaa

--插入测试数据
insert into testaa 
select  1,'A',100 union all
select  2,'A',200 union all
select  3,'A',300 union all
select  1,'B',1000 union all
select  2,'B',500 union all
select  1,'C',10000 union all
select  4,'A',50  

--查询按组累加
select  id,[group],num,sum(num) over(partition by [group] order by id) from testaa
group by  id,[group],num

/*

id          group      num         
----------- ---------- ----------- -----------
1           A          100         100
2           A          200         300
3           A          300         600
4           A          50          650
1           B          1000        1000
2           B          500         1500
1           C          10000       10000

(7 row(s) affected)
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值