日期 值
2006-06 35
2006-07 93
2006-08 10
2006-09 11
统计结果:
日期 值 值1
2006-06 35 35
2006-07 93 128
2006-08 10 138
2006-09 11 149就是 值1=上一个[值]和本次[值]的之和
2006-06 35
2006-07 93
2006-08 10
2006-09 11
统计结果:
日期 值 值1
2006-06 35 35
2006-07 93 128
2006-08 10 138
2006-09 11 149就是 值1=上一个[值]和本次[值]的之和
(
date char(7),
value int
)
insert into @test
select '2006-06',35 union all
select '2006-07',93 union all
select '2006-08',10 union all
select '2006-09',11select *,(select sum(value) from @test where date<=a.date)
from @test a/*
date value
------- ----------- -----------
2006-06 35 35
2006-07 93 128
2006-08 10 138
2006-09 11 149
*/
declare @tab table(日期 datetime,值 int)insert @tab values('2006-06-01',35)
insert @tab values('2006-07-01',92)
insert @tab values('2006-08-01',10)
insert @tab values('2006-09-01',11)
select a.*,[值1]=(select sum(值) from @tab where 日期<=a.日期) from @tab a