22,209
社区成员
发帖
与我相关
我的任务
分享
create table temp_a
(
num int ,
createdate datetime
)
insert into temp_a
select -1,'2018-01-20 00:19:31.000'
union select -1,'2018-01-13 00:19:46.000'
union select -1,'2018-01-06 00:17:03.000'
union select 4,'2017-12-30 17:15:13.000'
union select -1,'2017-12-30 01:01:53.000'
union select 4,'2017-12-27 16:04:30.000'
union select -1,'2017-12-23 00:24:03.000'
union select -1,'2017-12-16 00:15:02.000'
union select 4,'2017-12-15 16:25:02.000'
union select 4,'2017-12-15 16:14:37.000'
union select 2,'2017-12-13 12:34:34.807'
select * from temp_a order by createdate desc
SELECT *
,(SELECT SUM(num) FROM temp_a WHERE createdate<=a.createdate)
FROM temp_a AS a
ORDER BY createdate DESC;
SELECT a.*
, b.num2
FROM temp_a AS a
CROSS APPLY
(SELECT SUM(num) FROM temp_a WHERE createdate<=a.createdate) AS b(num2)
ORDER BY createdate DESC;
SELECT SUM(num)OVER(ORDER BY createdate),* FROM temp_a ORDER BY createdate DESC
SELECT ( SELECT SUM(num)
FROM temp_a b
WHERE b.createdate <= a.createdate
) ,
a.*
FROM temp_a a
ORDER BY a.createdate DESC