我有一个下面的表,我想要上一列的值。
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的最后一小时值。
询问
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。