NAME TYPE DATE
JACK 生日 1980
JACK 入职日 2000
JACK 离职日 2008
TOM 生日 1982
TOM 入职日 2013
TOM 离职日 2009
SAM 生日 1988
SAM 入职日 2006
以上是表 tableA
现求一SQL语句,查询此表中存在一个人离职日期小于入职日期的记录,比如此表中TOM的离职日期比入职日期小,需要找出。
十分感谢!
------解决方案--------------------
有索引的话,无需优化
另外一种思路
select *
from tableA f
where f.TYPE = '入职日'
and exists
(select 1
from tableA f2
where f2.NAME = f.NAME
and f2.TYPE = '离职日'
and f2.DATE < f.DATE);
------解决方案--------------------
with A_CODE as
(
select 'JACK' namea,'生日' TYPEa,'1980' DATEa from dual union all
select 'JACK' namea,'入职日' TYPEa,'2000' DATEa from dual union all
select 'JACK' namea,'离职日' TYPEa,'2008' DATEa from dual union all
select 'TOM' namea,'生日' TYPEa,'1982' DATEa from dual union all
select 'TOM' namea,'入职日' TYPEa,'2013' DATEa from dual union all
select 'TOM' namea,'离职日' TYPEa,'2009' DATEa from dual union all
select 'SAM' namea,'生日' TYPEa,'1988' DATEa from dual union all
select 'SAM' namea,'入职日' TYPEa,'2006' DATEa from dual
)
select *
from (select tt.namea,
max(decode(TYPEa, '生日', DATEa, null)) as 生日,