来自Emp
表的数据:
NUM DEPT HIRED_SDT HIRED_EDT
--- ----- ------------------------------ ------------------------------
1 Sales October, 01 2012 11:00:00+0000 October, 01 2012 11:30:00+0000
2 Sales October, 01 2012 11:22:00+0000 October, 01 2012 12:40:00+0000
3 Sales October, 01 2012 13:00:00+0000 October, 01 2012 13:50:00+0000
4 CC October, 01 2012 14:00:00+0000 October, 01 2012 15:00:00+0000
5 CC October, 01 2012 15:00:00+0000 October, 01 2012 16:10:00+0000
6 CC October, 01 2012 16:00:00+0000 October, 01 2012 17:20:00+0000
期望的结果:
仅按部门分组的重叠时间范围
==>例如。在Sales
内,有3个记录中有2个重叠
NUM DEPT HIRED_SDT HIRED_EDT
--- ----- ------------------------------ ------------------------------
1 Sales October, 01 2012 11:00:00+0000 October, 01 2012 11:30:00+0000
2 Sales October, 01 2012 11:22:00+0000 October, 01 2012 12:40:00+0000
5 CC October, 01 2012 15:00:00+0000 October, 01 2012 16:10:00+0000
6 CC October, 01 2012 16:00:00+0000 October, 01 2012 17:20:00+0000
答案 0 :(得分:2)
您可以尝试使用EXISTS
:
SELECT *
FROM Emp e
WHERE EXISTS (
SELECT *
FROM Emp
WHERE DEPT = e.DEPT
AND HIRED_SDT < e.HIRED_EDT
AND HIRED_EDT > e.HIRED_sDT
AND NUM <> e.NUM
);
即。如果存在另一行属于同一个部门并且该行与该行重叠的行,则选择一行。