这似乎有点令人费解,所以我会对改进感兴趣。
select distinct emp_id,
nvl(x_start_date,
lag(x_start_date)
over (partition by emp_id
order by rn)) as start_date,
nvl(x_end_date,
lead(x_end_date)
over (partition by emp_id
order by rn nulls first))
as end_date,
rating,
department
from (
select emp_id, start_date, end_date, rating, department,
case start_date
when lag(end_date)
over (partition by emp_id, rating, department
order by start_date) then null
else start_date end as x_start_date,
case end_date
when lead(start_date)
over (partition by emp_id, rating, department
order by start_date) then null
else end_date end as x_end_date,
rownum as rn
from table1
)
where x_start_date is not null or x_end_date is not null
order by emp_id, start_date
/
有了这个测试数据:
EMP_ID START_DA END_DATE RA DEPARTMENT SALARY
---------- -------- -------- -- -------------------- ----------
2000 01012010 01012011 A HR 9000
2000 01012011 01012012 A HR 10000
2000 01012012 01012013 A+ HR 20000
2000 01012013 01012014 A HR 20000
2000 01012014 12319999 A HR 21000
3000 01012011 01012012 B Operations 50000
3000 01012012 12319999 B Operations 60000
4000 07012011 07012012 B Operations 50000
4000 07012012 07012013 B Operations 50000
4000 07012013 12319999 B Operations 60000
我明白了:
EMP_ID START_DA END_DATE RA DEPARTMENT
---------- -------- -------- -- --------------------
2000 01012010 01012012 A HR
2000 01012012 01012013 A+ HR
2000 01012013 12319999 A HR
3000 01012011 12319999 B Operations
4000 07012011 12319999 B Operations
我还尝试了具有三个连续日期范围的emp_id
( 4000
) ,并且它处理了 OK - 外部where
子句使中间条目基本上消失了。编辑添加:现在也适用于您的附加日期范围2000/A
,因为我修复了外部lead
/lag
分区中的排序。
内部查询将除第一个开始日期和最后一个结束日期之外的所有内容都清除为连续块,外部查询使用第二轮lead
andlag
将它们合并到相同的行中,distinct
然后折叠。
我假设start_date
andend_date
是DATE
字段,而不是VARCHAR2
,并且您已NLS_DATE_FORMAT
设置为MMDDYYYY
. 如果它们存储为字符串,这是一个坏主意,您需要to_date()
在很多地方使排序正常工作。