奇怪,之前我发的只是Employeeid=2的数据没有取出来,怎么可以都为空呢--创建一个临时表create table TMPYY( EMPLOYEEID NUMBER, CURDATE DATE, MATCHCOUNT NUMBER, SCDATE DATE);--放数据insert into TMPYY values (1,to_date('2010-04-01', 'yyyy-mm-dd'),0,null);insert into TMPYY values (1,to_date('2010-04-02', 'yyyy-mm-dd'),1,null);insert into TMPYY values (1,to_date('2010-04-03', 'yyyy-mm-dd'),0,null);insert into TMPYY values (1,to_date('2010-04-04', 'yyyy-mm-dd'),0,null);insert into TMPYY values (1,to_date('2010-04-05', 'yyyy-mm-dd'),0,null);insert into TMPYY values (1,to_date('2010-04-06', 'yyyy-mm-dd'),3,null);insert into TMPYY values (1,to_date('2010-04-07', 'yyyy-mm-dd'),0,null);insert into TMPYY values (1,to_date('2010-04-08', 'yyyy-mm-dd'),4,null);insert into TMPYY values (2,to_date('2010-04-01', 'yyyy-mm-dd'),0,null);insert into TMPYY values (2,to_date('2010-04-02', 'yyyy-mm-dd'),0,null);insert into TMPYY values (2,to_date('2010-04-03', 'yyyy-mm-dd'),0,null);insert into TMPYY values (2,to_date('2010-04-04', 'yyyy-mm-dd'),0,null);insert into TMPYY values (2,to_date('2010-04-05', 'yyyy-mm-dd'),3,null);insert into TMPYY values (2,to_date('2010-04-06', 'yyyy-mm-dd'),5,null);insert into TMPYY values (2,to_date('2010-04-07', 'yyyy-mm-dd'),0,null);insert into TMPYY values (2,to_date('2010-04-08', 'yyyy-mm-dd'),1,null);insert into TMPYY values (10,to_date('2010-04-01', 'yyyy-mm-dd'),2,null);insert into TMPYY values (10,to_date('2010-04-02', 'yyyy-mm-dd'),1,null);insert into TMPYY values (10,to_date('2010-04-03', 'yyyy-mm-dd'),3,null);insert into TMPYY values (10,to_date('2010-04-04', 'yyyy-mm-dd'),0,null);insert into TMPYY values (10,to_date('2010-04-05', 'yyyy-mm-dd'),3,null);insert into TMPYY values (10,to_date('2010-04-06', 'yyyy-mm-dd'),0,null);insert into TMPYY values (10,to_date('2010-04-07', 'yyyy-mm-dd'),0,null);insert into TMPYY values (10,to_date('2010-04-08', 'yyyy-mm-dd'),0,null); --找到最近的日期update TMPYY tmp set tmp.scdate = (select min(curdate) from TMPYY tt where tt.employeeid = tmp.employeeid and tt.matchcount = 0 and tt.curdate < tmp.curdate and tt.curdate > nvl((select max(curdate) from TMPYY kk where kk.employeeid = tmp.employeeid and kk.matchcount > 0 and kk.curdate < tmp.curdate), to_date('2010-01-01', 'yyyy-mm-dd'))) where tmp.matchcount = 0; select * from TMPYY; 结果 EMPLOYEEID CURDATE MATCHCOUNT SCDATE1 2010-04-01 0 1 2010-04-02 1 1 2010-04-03 0 1 2010-04-04 0 2010-04-031 2010-04-05 0 2010-04-031 2010-04-06 3 1 2010-04-07 0 1 2010-04-08 4 2 2010-04-01 0 2 2010-04-02 0 2010-04-012 2010-04-03 0 2010-04-012 2010-04-04 0 2010-04-012 2010-04-05 3 2 2010-04-06 5 2 2010-04-07 0 2 2010-04-08 1 10 2010-04-01 2 10 2010-04-02 1 10 2010-04-03 3 10 2010-04-04 0 10 2010-04-05 3 10 2010-04-06 0 10 2010-04-07 0 2010-04-0610 2010-04-08 0 2010-04-06 --删除多余数据 delete TMPYY tmp where tmp.matchcount<>0 or tmp.scdate is null; select * from TMPYY;结果 1 2010-04-04 0 2010-04-031 2010-04-05 0 2010-04-032 2010-04-02 0 2010-04-012 2010-04-03 0 2010-04-012 2010-04-04 0 2010-04-0110 2010-04-07 0 2010-04-0610 2010-04-08 0 2010-04-06 --返回数据select tmp.employeeid, tmp.scdate StartDate, max(tmp.curdate) EndDate from TMPYY tmp where tmp.curdate - tmp.scdate + 1 >= 3 group by tmp.employeeid, tmp.scdate order by tmp.employeeid;结果1 2010-04-03 2010-04-052 2010-04-01 2010-04-0410 2010-04-06 2010-04-08