日期 金额
1-1 10
1-3 -2
1-9 1
1-20 20
1-22 -10
---》
日期 总金额
1-1 10
1-3 8
1-9 9
1-20 29
1-22 19也就是说出来的数据,是前面几项sum起来的,要怎么弄呢?
能不能一个视图就能搞定呢?
1-1 10
1-3 -2
1-9 1
1-20 20
1-22 -10
---》
日期 总金额
1-1 10
1-3 8
1-9 9
1-20 29
1-22 19也就是说出来的数据,是前面几项sum起来的,要怎么弄呢?
能不能一个视图就能搞定呢?
*************************************
* T-MAC 小编 *
* -->努力成长中 *
* -->梦想DBA *
*************************************
*/
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (日期 varchar(10),金额 int)
insert tb select
'1-01', 10 union select
'1-03', -2 union select
'1-09', 1 union select
'1-20', 20 union select
'1-22', -10
go
select 日期,
总金额=case when not exists(select * from tb where k.日期>日期) then 金额 else
(select SUM(金额) from tb where k.日期>=日期) end
from tb k(5 行受影响)
日期 总金额
---------- -----------
1-01 10
1-03 8
1-09 9
1-20 29
1-22 19
drop table tb
go
create table tb (日期 varchar(10),金额 int)
insert tb select
'1-01', 10 union select
'1-03', -2 union select
'1-09', 1 union select
'1-20', 20 union select
'1-22', -10
go
select 日期,
总金额=
(select SUM(金额) from tb where k.日期>=日期)
from tb k(所影响的行数为 5 行)日期 总金额
---------- -----------
1-01 10
1-03 8
1-09 9
1-20 29
1-22 19(所影响的行数为 5 行)?
as
select [日期],(select sum([金额]) from ta where datediff(d,[日期],a.[日期])>=0) as t
from ta a
go
select * from v_test