连接查询 where row_number +1
我的表包含行,其中 [time_start] > [时间结束] 我想重写查询,以便值 [time_start] 和 [time_end] 正确显示,如下所示。
14, 2022-03-16, 2022-03-16 23:30:33.000, 2022-03-17 00:36:52.000
14, 2022-03-17, 2022-03-17 23:31:00.000, 2022-03-18 00:59:38.000
14, 2022-03-18, NULL ,NULL
14, 2022-03-19, 2022-03-19 23:30:51.000, 2022-03-19 23:38:05.000
我尝试通过 ROW_NUMBER 连接,但我需要考虑上面的 +1 行条件。 如何解决这个问题? 谢谢
CREATE TABLE [dbo].[#tmp_time](
[num_wf] [varchar](50) NULL,
[dt] [date] NULL,
[time_start] [datetime] NULL,
[time_end] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-16', '20220316 23:30:33', '20220316 01:23:40')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-17', '20220317 23:31:00', '20220317 00:36:52')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-18', NULL, '20220318 00:59:38')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-19', '20220319 23:30:51', '20220319 23:38:05')
select t1.[num_wf], t1.[dt], t1.[time_start], t2.[time_end]
from (
select [num_wf],[dt], [time_start]
,ROW_NUMBER() OVER (PARTITION BY [num_wf] ORDER BY [dt], [time_start]) as rn_start
from [dbo].[#tmp_time]
)t1
LEFT JOIN (
select [num_wf],[time_end]
,ROW_NUMBER() OVER (PARTITION BY [num_wf] ORDER BY [dt], [time_end]) as rn_end
from [dbo].[#tmp_time]
)t2 ON t1.[num_wf]=t2.[num_wf] AND t1.[rn_start]=t2.[rn_end] --AND t1.[time_start] < t2.[time_end]
My table contains rows, where [time_start] > [time_end]
I want to rewrite the query so that the values [time_start] and [time_end] displayed correctly as shown below.
14, 2022-03-16, 2022-03-16 23:30:33.000, 2022-03-17 00:36:52.000
14, 2022-03-17, 2022-03-17 23:31:00.000, 2022-03-18 00:59:38.000
14, 2022-03-18, NULL ,NULL
14, 2022-03-19, 2022-03-19 23:30:51.000, 2022-03-19 23:38:05.000
I tried to connect via ROW_NUMBER, but I need to take into account the +1 row condition above.
How can this be resolved?
Thanks
CREATE TABLE [dbo].[#tmp_time](
[num_wf] [varchar](50) NULL,
[dt] [date] NULL,
[time_start] [datetime] NULL,
[time_end] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-16', '20220316 23:30:33', '20220316 01:23:40')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-17', '20220317 23:31:00', '20220317 00:36:52')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-18', NULL, '20220318 00:59:38')
INSERT INTO [dbo].[#tmp_time] ([num_wf],[dt],[time_start],[time_end])
VALUES (14, '2022-03-19', '20220319 23:30:51', '20220319 23:38:05')
select t1.[num_wf], t1.[dt], t1.[time_start], t2.[time_end]
from (
select [num_wf],[dt], [time_start]
,ROW_NUMBER() OVER (PARTITION BY [num_wf] ORDER BY [dt], [time_start]) as rn_start
from [dbo].[#tmp_time]
)t1
LEFT JOIN (
select [num_wf],[time_end]
,ROW_NUMBER() OVER (PARTITION BY [num_wf] ORDER BY [dt], [time_end]) as rn_end
from [dbo].[#tmp_time]
)t2 ON t1.[num_wf]=t2.[num_wf] AND t1.[rn_start]=t2.[rn_end] --AND t1.[time_start] < t2.[time_end]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面将为您提供预期的结果。
它使用
LEAD()
窗口函数来获取下一行time_end
值Below will gives you the expected result.
It uses
LEAD()
window function to obtain the next rowtime_end
value