求出重复的项目的日期

SQL> select  *from emp_project;
EMPNO ENAME     PROJ_ID PROJ_START PROJ_END
---------- ------ ---------- ---------- ----------
7782 CLARK           1 2005-06-16 2005-06-18
7782 CLARK           4 2005-06-19 2005-06-24
7782 CLARK           7 2005-06-22 2005-06-25
7782 CLARK          10 2005-06-25 2005-06-28
7782 CLARK          13 2005-06-28 2005-07-02
7839 KING            2 2005-06-17 2005-06-21
7839 KING            8 2005-06-23 2005-06-25
7839 KING           14 2005-06-29 2005-06-30
7839 KING           11 2005-06-26 2005-06-27
7839 KING            5 2005-06-20 2005-06-24
7934 MILLER          3 2005-06-18 2005-06-22
7934 MILLER         12 2005-06-27 2005-06-28
7934 MILLER         15 2005-06-30 2005-07-03
7934 MILLER          9 2005-06-24 2005-06-27
7934 MILLER          6 2005-06-21 2005-06-23
15 rows selected.

 

 

-- 求出重复的项目日期

 

select  t.empno,
t.ename,
t.proj_id,
t.proj_start,
t.proj_end,
case when proj_start <= lastProj_endDate
then proj_id || ' and ' || lastProj_endEmpno
end rep
from (
select e.*,
lag(proj_end) over (partition by empno order by proj_start) lastProj_endDate,
lag(proj_id) over (partition by empno order by proj_start) lastProj_endEmpno
from emp_project e
) t
where proj_start <= lastProj_endDate
EMPNO ENAME     PROJ_ID PROJ_START PROJ_END   REP
---------- ------ ---------- ---------- ---------- ----------
7782 CLARK           7 2005-06-22 2005-06-25 7 and 4
7782 CLARK          10 2005-06-25 2005-06-28 10 and 7
7782 CLARK          13 2005-06-28 2005-07-02 13 and 10
7839 KING            5 2005-06-20 2005-06-24 5 and 2
7839 KING            8 2005-06-23 2005-06-25 8 and 5
7934 MILLER          6 2005-06-21 2005-06-23 6 and 3
7934 MILLER         12 2005-06-27 2005-06-28 12 and 9
7 rows selected.

 

-- 自关联

SELECT a.empno,
a.ename,
'(工程 ' || lpad(b.proj_id, 2, '0') || ')与工程 ( ' || lpad(a.proj_id, 2, '0') || ')重复' AS msg
FROM emp_project a, emp_project b
WHERE a.empno = b.empno /*限定为员工本人数据关联*/
AND b.proj_start >= a.proj_start /*b开始时间在a起止时间内*/
AND b.proj_start <= a.proj_end
AND a.proj_id != b.proj_id /*非同一条数据*/
ORDER BY a.empno, a.proj_start;

 

你可能感兴趣的:(oracle,sql)