我们从产品的遗留应用程序接收数据,表的数据如下所示
Table 1 (KEY1, ID,Valid_From, Valid_To, IS_LATEST_FLAG)
'1', 'abc', '3/2/2020 12:00:00 AM', '3/3/2020 6:01:12 PM', 'N'
'2', 'abc', '3/3/2020 6:01:13 PM', '3/3/2020 6:01:14 PM', 'N'
'3', 'abc', '3/3/2020 6:01:15 PM', '3/3/2020 6:01:15 PM', 'N'
'4', 'abc', '3/3/2020 6:01:16 PM', '3/3/2020 6:01:51 PM', 'N'
'5', 'abc', '3/3/2020 6:01:52 PM', '3/4/2020 4:28:59 PM', 'N'
'6', 'abc', '3/4/2020 4:29:00 PM', '3/4/2020 4:31:40 PM', 'N'
'7', 'abc', '3/4/2020 4:31:41 PM', '12/31/4712', 'Y'
'8', 'bbc', '3/19/2020 2:47:08 PM', '3/19/2020 2:50:36 PM', 'N'
'9', 'bbc', '3/19/2020 2:50:37 PM', '3/19/2020 2:56:23 PM', 'N'
'10', 'bbc', '3/19/2020 2:56:24 PM', '12/31/4712', 'Y'
Table 2 (KEY2, ID,Valid_From, Valid_To, IS_LATEST_FLAG)
'1', 'abc', '3/3/2020 12:00:00 AM', '3/3/2020 6:01:13 PM', 'N'
'2', 'abc', '3/3/2020 6:01:14 PM' '3/3/2020 6:01:14 PM', 'N'
'3', 'abc', '3/3/2020 6:01:15 PM' '3/4/2020 4:28:59 PM', 'N'
'4', 'abc', '3/4/2020 4:29:00 PM' '12/31/4712', 'Y'
'5', 'bbc', '3/4/2020 4:31:41 PM' '3/19/2020 2:47:07 PM','N'
'6', 'bbc', '3/19/2020 2:47:08 PM' '3/19/2020 2:50:36 PM','N'
'7', 'bbc', '3/19/2020 2:50:37 PM' '3/20/2020 2:56:23 PM','N'
'8', 'bbc', '3/20/2020 2:56:24 PM' '12/31/4712', 'Y'
我们需要为两个表创建一个超集,其中包含两个表中所有不同的有效起始日期,并且记录结束日期将自动比下一个记录短1秒,这样就不会有重叠的日期或间隔。最具挑战性的部分是为这个时间段找到合适的关键点。
输出表如下所示。我试过联合,但效果不尽如人意。看看下面是如何实现的。
Table 2 (KEY1,KEY2, ID,Valid_From, Valid_To, IS_LATEST_FLAG)
'1','NULL', 'abc', '3/2/2020 12:00:00 AM', '3/2/2020 11:59:59 PM', 'N'
'1','1', 'abc', '3/3/2020 12:00:00 AM' '3/2/2020 6:01:12 PM', 'N'
'2','1', 'abc', '3/3/2020 6:01:13 PM' '3/3/2020 6:01:13 PM', 'N'
'2','2', 'abc', '3/3/2020 6:01:14 PM' '3/3/2020 6:01:14 PM', 'N'
'3','3', 'abc', '3/3/2020 6:01:15 PM' '3/3/2020 6:01:15 PM', 'N'
'4','3', 'abc', '3/3/2020 6:01:16 PM' '3/3/2020 6:01:51 PM', 'N'
'5','3', 'abc', '3/3/2020 6:01:52 PM', '3/4/2020 4:28:59 PM', 'N'
'6','4', 'abc', '3/4/2020 4:29:00 PM', '3/4/2020 4:31:40 PM', 'N'
'7','4', 'abc', '3/4/2020 4:31:41 PM', '12/31/4712', 'Y'
'NULL','5', 'bbc', '3/4/2020 4:31:41 PM' '3/19/2020 2:47:07 PM', 'N'
'8','6', 'bbc', '3/19/2020 2:47:08 PM' '3/19/2020 2:50:36 PM', 'N'
'9','7', 'bbc', '3/19/2020 2:50:37 PM' '3/20/2020 2:56:23 PM', 'N'
'10','8', 'bbc', '3/20/2020 2:56:24 PM' '12/31/4712', 'Y'
1条答案
按热度按时间z9gpfhce1#
看起来这就是你需要的:
完整示例和示例数据:
结果: