SQLServer2008 关于数值字段列的累计

create table #temp20110610
(
     id int identity(1,1),
     date varchar(8),
     qty float
)

insert into #temp20110610
select '20110401',40 union all
select '20110402',64 union all
select '20110403',56 union all
select '20110404',346 union all
select '20110405',56 union all
select '20110406',678 union all
select '20110407',1234 union all
select '20110408',560 union all
select '20110409',460 union all
select '20110410',566 union all
select '20110411',344 union all
select '20110412',77 union all
select '20110413',90

declare @SDate varchar(8),@EDate varchar(8),@conditionQty float
set @SDate='20110405'
set @EDate='20110411'
set @conditionQty=3000
--得到指定日期范围类,Qty列的累积数量大于3000
select id,date,qty,
(select SUM(qty) from #temp20110610 where date<=A.date and date between @SDate and @EDate) Qty累计量
from #temp20110610 A
where (select SUM(qty) from #temp20110610 where date<=A.date and date between @SDate and @EDate)<
=@conditionQty
and date between @SDate and @EDate
order by A.date

 

 

 

posted @ 2016-03-11 14:48  已注销1  阅读(415)  评论(0编辑  收藏  举报