开发者社区> 问答> 正文

如何在日期更改时从SQL Server检索上一行的值

我有一个下面的表,我想要上一列的值。

row     Planned     Site    Unit    Datetime
Number  Hours       Name    Name
===============================================================
1       0.0000000   Site    Unit    2019-07-06 23:59:59.0000000
2       0.0000000   Site    Unit    2019-07-06 23:00:00.0000000
3       0.0000000   Site    Unit    2019-07-06 22:00:00.0000000
4       0.0000000   Site    Unit    2019-07-06 21:00:00.0000000
5       0.0000000   Site    Unit    2019-07-06 20:00:00.0000000
6       0.0000000   Site    Unit    2019-07-06 19:00:00.0000000
7       0.0000000   Site    Unit    2019-07-06 18:00:00.0000000
8       0.0000000   Site    Unit    2019-07-06 17:00:00.0000000
9       0.0000000   Site    Unit    2019-07-06 16:00:00.0000000
10      0.0000000   Site    Unit    2019-07-06 15:00:00.0000000
11      0.0000000   Site    Unit    2019-07-06 14:00:00.0000000
12      0.0000000   Site    Unit    2019-07-06 13:00:00.0000000
13      0.0000000   Site    Unit    2019-07-06 12:00:00.0000000
14      0.0000000   Site    Unit    2019-07-06 11:00:00.0000000
15      0.0000000   Site    Unit    2019-07-06 10:00:00.0000000
16      0.0000000   Site    Unit    2019-07-06 09:00:00.0000000
17      0.0000000   Site    Unit    2019-07-06 08:00:00.0000000
18      0.0000000   Site    Unit    2019-07-06 07:00:00.0000000
19      0.0000000   Site    Unit    2019-07-06 06:00:00.0000000
20      0.0000000   Site    Unit    2019-07-06 05:00:00.0000000
21      0.0000000   Site    Unit    2019-07-06 04:35:00.0000000
22      0.0000000   Site    Unit    2019-07-06 04:00:00.0000000
23      0.0000000   Site    Unit    2019-07-06 03:00:00.0000000
24      0.0000000   Site    Unit    2019-07-06 02:16:00.0000000
25      0.0000000   Site    Unit    2019-07-06 02:00:00.0000000
26      0.0000000   Site    Unit    2019-07-06 01:00:00.0000000
27      0.0000000   Site    Unit    2019-07-06 00:00:00.0000000
28      0.0000000   Site    Unit    2019-07-05 23:00:00.0000000
29      0.0000000   Site    Unit    2019-07-05 22:00:00.0000000
30      0.0000000   Site    Unit    2019-07-05 21:00:00.0000000
31      0.0000000   Site    Unit    2019-07-05 20:00:00.0000000
32      0.0000000   Site    Unit    2019-07-05 19:00:00.0000000
33      0.0000000   Site    Unit    2019-07-05 18:00:00.0000000
34      0.0000000   Site    Unit    2019-07-05 17:00:00.0000000
35      0.0000000   Site    Unit    2019-07-05 16:00:00.0000000
36      0.0000000   Site    Unit    2019-07-05 15:00:00.0000000
37      0.0000000   Site    Unit    2019-07-05 14:00:00.0000000
38      0.0000000   Site    Unit    2019-07-05 13:00:00.0000000
39      0.0000000   Site    Unit    2019-07-05 12:00:00.0000000
40      0.0000000   Site    Unit    2019-07-05 11:00:00.0000000
41      0.0000000   Site    Unit    2019-07-05 10:00:00.0000000
42      0.0000000   Site    Unit    2019-07-05 09:00:00.0000000
43      0.0000000   Site    Unit    2019-07-05 08:00:00.0000000
44      0.0000000   Site    Unit    2019-07-05 07:00:00.0000000
45      0.6000000   Site    Unit    2019-07-05 06:40:00.0000000
46      0.0000000   Site    Unit    2019-07-05 06:01:00.0000000
47      0.0000000   Site    Unit    2019-07-05 06:00:00.0000000
48      0.0000000   Site    Unit    2019-07-05 05:00:00.0000000
49      0.0000000   Site    Unit    2019-07-05 04:00:00.0000000
50      0.0000000   Site    Unit    2019-07-05 03:00:00.0000000
51      0.0000000   Site    Unit    2019-07-05 02:00:00.0000000
52      0.0000000   Site    Unit    2019-07-05 01:00:00.0000000
53      0.7000000   Site    Unit    2019-07-05 00:43:00.0000000
54      0.0000000   Site    Unit    2019-07-05 00:00:00.0000000

我在PlannedHours上使用了LAG函数。但是当在datetime上使用的where子句的上一个Plannedours值是null时,但是我想要上一个datetime的最后一小时值。

展开
收起
祖安文状元 2020-01-05 14:20:23 431 0
1 条回答
写回答
取消 提交回答
  • 询问

    SELECT * FROM
    (
       SELECT *,LEAD([Planned Hours], 1,0) OVER (ORDER BY [row Number]) [Next Row] 
       FROM Tbl
    ) X WHERE [Planned Hours] <> [Next Row] AND [Next Row] IS NOT NULL
    
    

    输出将是

    row     Planned     Site    Unit    Datetime
    Number  Hours       Name    Name
    ===============================================================
    52      0.0000000   Site    Unit    2019-07-05 01:00:00.0000000
    53      0.7000000   Site    Unit    2019-07-05 00:43:00.0000000
    
    2020-01-05 14:20:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载