我试图总结一个员工表,其中员工在一个团队中时存在多条记录。我曾尝试按、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