--测试环境
declare @t table(id int identity(1,1),date varchar(10),qty int,type varchar(10))
insert into @t select '2005-7-2',4,'A'
union all select '2005-7-4',5,'A'
union all select '2005-7-6',9,'A'
union all select '2005-7-8',13,'B'
union all select '2005-7-12',21,'B'
union all select '2005-8-3',40,'C'
union all select '2005-8-6',50,'C'
--查询语句
select
*,
allqty=(select sum(qty) from @t where month(date)=month(A.date) and type=A.type and id<=A.id)
from @t A--结果
id date qty type allqty
----------- ---------- ----------- ---------- -----------
1 2005-7-2 4 A 4
2 2005-7-4 5 A 9
3 2005-7-6 9 A 18
4 2005-7-8 13 B 13
5 2005-7-12 21 B 34
6 2005-8-3 40 C 40
7 2005-8-6 50 C 90(所影响的行数为 7 行)
declare @t table(id int identity(1,1),date varchar(10),qty int,type varchar(10))
insert into @t select '2005-7-2',4,'A'
union all select '2005-7-4',5,'A'
union all select '2005-7-6',9,'A'
union all select '2005-7-8',13,'B'
union all select '2005-7-12',21,'B'
union all select '2005-8-3',40,'C'
union all select '2005-8-6',50,'C'
--查询语句
select
*,
allqty=(select sum(qty) from @t where month(date)=month(A.date) and type=A.type and id<=A.id)
from @t A--结果
id date qty type allqty
----------- ---------- ----------- ---------- -----------
1 2005-7-2 4 A 4
2 2005-7-4 5 A 9
3 2005-7-6 9 A 18
4 2005-7-8 13 B 13
5 2005-7-12 21 B 34
6 2005-8-3 40 C 40
7 2005-8-6 50 C 90(所影响的行数为 7 行)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货