日期:2014-05-17 浏览次数:20764 次
with temp as( select '100280' empid,1 A,'EL' B,25729 C,'IN' D from dual union all select '100280' empid,1 A,'EL' B,25729 C,'OUT' D from dual union all select '100380' empid,1 A,'EL' B,22613 C,'OUT' D from dual union all select '100380' empid,1 A,'EL' B,733 C,'IN' D from dual union all select '100489' empid,1 A,'EL' B,28606 C,'OUT' D from dual union all select '100489' empid,1 A,'EL' B,20843 C,'IN' D from dual ) select empid,A,B,C,D from temp t3 where t3.empid in ( select t1.empid from temp t1,temp t2 where t1.empid = t2.empid and t1.D = 'IN' and t2.D = 'OUT' and t1.C < t2.C ) order by empid,D desc
------解决方案--------------------
--试一下 with tt as( select '100280' col1, 1 col2, 'EL' col3, 25729 col4,'IN' state, date'2010-01-01' time from dual union all select '100280' col1, 1 col2, 'EL' col3, 25729 col4,'OUT' state, date'2010-01-03' time from dual union all select '100380' col1, 1 col2, 'EL' col3, 22613 col4,'OUT' state, date'2010-01-04' time from dual union all select '100380' col1, 1 col2, 'EL' col3, 733 col4,'IN' state, date'2010-01-05' time from dual union all select '100489' col1, 1 col2, 'EL' col3, 28606 col4,'OUT' state, date'2010-01-06' time from dual union all select '100489' col1, 1 col2, 'EL' col3, 20843 col4,'IN' state, date'2010-01-07' time from dual) SELECT * FROM (SELECT tt.* FROM tt ORDER BY TIME) WHERE NOT (LEVEL = 1 AND connect_by_isleaf = 1) START WITH TIME = (SELECT MIN(TIME) FROM test WHERE state = 'OUT') CONNECT BY PRIOR TIME < TIME AND PRIOR decode(state, 'OUT', 1, -1) = -1 * decode(state, 'OUT', 1, -1) AND PRIOR col1 = col1 AND PRIOR col2 = col2 AND PRIOR col3 = col3 AND PRIOR col4 <> col4