【发布时间】:2019-07-11 10:19:38
【问题描述】:
我正在尝试汇总一个员工表,其中一个员工在一个团队中时存在多条记录。我曾尝试按、Min/Max Over Partition By 和 Lead/Lag 团队名称进行分组,但每个结果都以一个代理结束,该代理已从一个团队移动,然后在稍后的日期作为一次事件返回到原始团队组,即使我正在按日期排序。
示例数据库:
Employee Name | Employee ID | Team Leader | Location | Start Date | End Date
John Smith | 123123 | Team A | Site A | 01/JAN/19 | 02/JAN/19
John Smith | 123123 | Team A | Site A | 02/JAN/19 | 03/JAN/19
John Smith | 123123 | Team B | Site A | 03/JAN/19 | 04/JAN/19
John Smith | 123123 | Team A | Site A | 04/JAN/19 | 05/JAN/19
John Smith | 123123 | Team B | Site A | 05/JAN/19 | 06/JAN/19
当我运行示例查询时:
SELECT
Employee Name
,Employee ID
,Team Leader
,Location
,MIN(Start Date) OVER(PARTITION BY Team Leader ORDER BY Employee ID, Start Date) AS Starting Date
,MAX(End Date) OVER(PARTITION BY Team Leader ORDER BY Employee ID, End Date) AS End Date
FROM TABLE 1
结果如下:
Employee Name | Employee ID | Team Leader | Location | Start Date | End Date
John Smith | 123123 | Team A | Site A | 01/JAN/19 | 05/JAN/19
John Smith | 123123 | Team B | Site A | 03/JAN/19 | 06/JAN/19
任何人都可以帮助实现预期的结果:
Employee Name | Employee ID | Team Leader | Location | Start Date | End Date
John Smith | 123123 | Team A | Site A | 01/JAN/19 | 03/JAN/19
John Smith | 123123 | Team B | Site A | 03/JAN/19 | 04/JAN/19
John Smith | 123123 | Team A | Site A | 04/JAN/19 | 05/JAN/19
John Smith | 123123 | Team B | Site A | 05/JAN/19 | 06/JAN/19
【问题讨论】:
-
寻找缝隙和孤岛。很多帖子都可以找到
-
这是一个重复的问题,尽管我很难找到最适合这里的答案。解决方案类似于here。
-
@PonderStibbons 我从您提供的链接中测试了类似的逻辑,它似乎有效。我将验证何时将更多员工添加到查询中。谢谢!
标签: sql oracle gaps-and-islands