我得到了这个SCD表:
| 开始日期|结束日期|分区|
| - ------|- ------|- ------|
| 2022年3月8日15:35:09.856|2022年3月9日14时57分36.610秒|1个|
| 2022年3月9日14时57分36.610秒|2022年5月18日13时26分31.195秒|第二章|
| 2022年5月18日13时26分31.195秒|2022年8月2日10时12分02秒441|第二章|
| 2022年8月2日10时12分02秒441|2022年9月1日11时10分01秒019|第二章|
| 2022年9月1日11时10分01秒019|2022年9月1日11时10分20秒777|1个|
| 2022年9月1日11时10分20秒777|2022年9月1日11时21分26.526秒|1个|
我想知道每个分区的start_date和end_date在另一个分区的最后一个值(只有两个)。对于给定的表:
| 开始日期|结束日期|分区|最大开始日期|最大结束日期|
| - ------|- ------|- ------|- ------|- ------|
| 2022年3月8日15:35:09.856|2022年3月9日14时57分36.610秒|1个|零|零|
| 2022年3月9日14时57分36.610秒|2022年5月18日13时26分31.195秒|第二章|2022年3月8日15:35:09.856|2022年3月9日14时57分36.610秒|
| 2022年5月18日13时26分31.195秒|2022年8月2日10时12分02秒441|第二章|2022年3月8日15:35:09.856|2022年3月9日14时57分36.610秒|
| 2022年8月2日10时12分02秒441|2022年9月1日11时10分01秒019|第二章|2022年3月8日15:35:09.856|2022年3月9日14时57分36.610秒|
| 2022年9月1日11时10分01秒019|2022年9月1日11时10分20秒777|1个|2022年8月2日10时12分02秒441|2022年9月1日11时10分01秒019|
| 2022年9月1日11时10分20秒777|2022年9月1日11时21分26.526秒|1个|2022年8月2日10时12分02秒441|2022年9月1日11时10分01秒019|
我尝试了一些last_value窗口函数,但没有成功。,last_value(start_date)OVER(按分区='1'按start_date asc排序)as last_start_date_partition,last_value(end_date)OVER(按分区='1'按end_date asc排序)as last_end_date_partition是否有任何选项可以将条件注入窗口函数并使其按此方式运行?
3条答案
按热度按时间fnx2tebb1#
使用
dense_rank
:See fiddle .
11dmarpk2#
使用窗口函数和gaps-and-islandish方法:
Fiddle
yacmzcpb3#
您可以执行自左连接和聚合,如下所示:
See demo