Employee表的问题是EMployeeID来了两次(人员id是唯一的(。正在创建返回员工id的单个数据的视图。用例:
EMPID PID Name StartDate End Date .......More Fields
217121 761331 Tefan 21-FEB-19 31-AUG-20
217121 767001 Tefan 01-SEP-20 null
602315 767002 Wolf 01-SEP-20 null
602315 764321 Wolf 01-DEC-15 31-AUG-20
766470 766472 Deva 14-JUL-20 31-DEC-22
- 带有单个记录的Emp ID应该是原样
- 带有两个或的Emp ID更多行:
- 考虑行:具有Startdate<=sysdate(仅限最近的记录(
- 考虑ENDDATE:来自ENDATE>sysdate或ENDDATE=null或最新
输出应为:
217121 761331 Tefan 21-FEB-19 null
602315 764321 Wolf 01-DEC-15 null
766470 766472 Deva 14-JUL-20 31-DEC-22
使用SQL/Oracle。
当前查询需要9分钟才能获取30000条记录。(50条记录需要5到10秒,这是一个很大的时间(;我建议使用不同的角度:
select * from
(select t1.*,(select t.enddate from (select enddate,empid,
ROW_NUMBER() OVER (PARTITION BY empid ORDER BY enddate desc) as seqnum
from employee t2
where t2.empid=t1.empid) t
where seqnum=1) As enddate_1 from (select * from
(select WED.*,ROW_NUMBER() OVER (PARTITION BY empid ORDER BY startdate desc) as seqnum from
(select t1.*,COUNT(*) OVER (PARTITION BY empid) WDECOUNT from employee t1) WED
where WDECOUNT=1 or WED.startdate <= sysdate)) t1 WHERE seqnum=1);
如果此断言为true;
对于每个EmployeeId,较晚的StartDate返回较高的PersonId
然后我可以建议使用一些MAX/MIN来提高查询效率。
像这样的东西;
SELECT DISTINCT e.EMPID, MAX(e.PID), e.Name, MIN(e.startdate),
MAX(e.enddate) keep (dense_rank first order by enddate desc nulls first)
FROM employee e
WHERE e.startdate <= SYSDATE
GROUP BY e.EMPID, e.Name;
编辑;包括WHERE起始日期<sysdate