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)
*/