34,593
社区成员
发帖
与我相关
我的任务
分享
create table tb([day] datetime,[number] int)
insert into tb
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8 union all
select '2008-11-11',8
select identity(int,1,1) id ,[day],number into #Temp from tb
select [day] , number = (select sum(number) from #Temp where id <= t.id) from #Temp t
drop table tb
drop table #Temp
--> 测试数据:@table
declare @table table([day] varchar(10),[number] int)
insert @table
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8
select t.day,sum(h.number) as number
from @table t join @table h
on convert(varchar(10),h.day,120) <= convert(varchar(10),t.day,120)
group by t.day
--结果
--------------------------
2009-10-11 5
2009-10-12 12
2009-11-11 20
抄dawugui的 加入2008时间
create table tb([day] datetime,[number] int)
insert into tb
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8 union all
select '2008-11-11',8
select [day] , number = (select sum(number) from tb where [day] <= t.[day]) from tb t
drop table tb
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-11-20 21:09:19
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([day] datetime,[number] int)
insert [tb]
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8
--------------开始查询--------------------------
select
t.[day],
(select sum(number) from (select *,id=row_number()over(order by getdate()) from tb) as b where [day] <=t.[day] ) as number
from
(select *,id=row_number()over(order by getdate()) from tb) t
----------------结果----------------------------
/* day nums
----------------------- -----------
2009-10-11 00:00:00.000 5
2009-10-12 00:00:00.000 12
2009-11-11 00:00:00.000 20
(3 行受影响)
*/
create table tb([day] datetime,[number] int)
insert into tb
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8
select [day] , number = (select sum(number) from tb where [day] <= t.[day]) from tb t
drop table tb
/*
day number
------------------------------------------------------ -----------
2009-10-11 00:00:00.000 5
2009-10-12 00:00:00.000 12
2009-11-11 00:00:00.000 20
(所影响的行数为 3 行)
*/
if object_ID('totle') is not null drop table totle
go
create table totle([day] datetime,number int)
go
insert into totle values('2009-10-11',5)
insert into totle values('2009-10-12 ',7)
insert into totle values('2009-11-11',8)
select a.*, (select sum(number) from totle as b where b.[day] <=a.[day] ) as nums
from totle as a
/*day number nums
----------------------- ----------- -----------
2009-10-11 00:00:00.000 5 5
2009-10-12 00:00:00.000 7 12
2009-11-11 00:00:00.000 8 20
(3 行受影响)
*/
楼主不要SELECT *
declare @tb table([day] datetime,[number] int)
insert @tb
select '2009-10-11',5 union all
select '2009-10-12',7 union all
select '2009-11-11',8
select [day],(select sum([number]) from @tb where t.[day]>=[day])
from @tb t
group by [day]
/*
day
------------------------------------------------------ -----------
2009-10-11 00:00:00.000 5
2009-10-12 00:00:00.000 12
2009-11-11 00:00:00.000 20
(所影响的行数为 3 行)