if not object_id('tb') is null drop table tb Go Create table tb([Date] decimal(18,1),[Qty] int) Insert tb select 1.1,50 union all select 1.2,100 union all select 1.3,70 union all select 1.4,1 Go select *, [all]=(select sum([Qty])from tb where [date]<=t.[date]) from tb t /* Date Qty all --------------------------------------- ----------- ----------- 1.1 50 50 1.2 100 150 1.3 70 220 1.4 1 221 */
select *,[Total]=(select sum([Qty]) from tb where [date]< = T.[date]) from tb T
Create table tb(Date decimal(18,1),Qty int) Insert tb select 1.1,50 union all select 1.2,100 union all select 1.3,70 union all select 1.4,1select * , Total = (select sum(Qty)from tb where date <= t.date) from tb tdrop table tb/* Date Qty Total -------------------- ----------- ----------- 1.1 50 50 1.2 100 150 1.3 70 220 1.4 1 221(所影响的行数为 4 行) */
if not object_id('tb') is null drop table tb Go Create table tb([Date] decimal(18,1),[Qty] int) Insert tb select 1.1,50 union all select 1.2,100 union all select 1.3,70 union all select 1.4,1 Go select *, (select sum([Qty])from tb where [date]<=t.[date]) 'total' from tb tDate Qty total --------------------------------------- ----------- ----------- 1.1 50 50 1.2 100 150 1.3 70 220 1.4 1 221(4 行受影响)
drop table tb
Go
Create table tb([Date] decimal(18,1),[Qty] int)
Insert tb
select 1.1,50 union all
select 1.2,100 union all
select 1.3,70 union all
select 1.4,1
Go
select *,
[all]=(select sum([Qty])from tb where [date]<=t.[date])
from tb t
/*
Date Qty all
--------------------------------------- ----------- -----------
1.1 50 50
1.2 100 150
1.3 70 220
1.4 1 221
*/
from tb where [date]< = T.[date])
from tb T
Insert tb
select 1.1,50 union all
select 1.2,100 union all
select 1.3,70 union all
select 1.4,1select * , Total = (select sum(Qty)from tb where date <= t.date) from tb tdrop table tb/*
Date Qty Total
-------------------- ----------- -----------
1.1 50 50
1.2 100 150
1.3 70 220
1.4 1 221(所影响的行数为 4 行)
*/
drop table tb
Go
Create table tb([Date] decimal(18,1),[Qty] int)
Insert tb
select 1.1,50 union all
select 1.2,100 union all
select 1.3,70 union all
select 1.4,1
Go
select *,
(select sum([Qty])from tb where [date]<=t.[date]) 'total'
from tb tDate Qty total
--------------------------------------- ----------- -----------
1.1 50 50
1.2 100 150
1.3 70 220
1.4 1 221(4 行受影响)