日期:2014-05-18 浏览次数:20687 次
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (theDate varchar(10),totalPrice int)
insert into [tb]
select '2011-9-23',1 union all
select '2011-9-23',2 union all
select '2011-9-23',22 union all
select '2011-9-22',32 union all
select '2011-9-22',2 union all
select '2011-9-21',9
--开始查询
select theDate,
totalPrice=(select sum(totalPrice) from [tb] where theDate<=a.theDate)
from [tb] a
group by theDate
order by 1 desc
--结束查询
drop table [tb]
/*
theDate totalPrice
---------- -----------
2011-9-23 68
2011-9-22 43
2011-9-21 9
(3 行受影响)