日期:2014-05-17 浏览次数:20940 次
create table tabs (dt date, status char(1)); insert into tabs values(timestamp '2010-02-05 10:25:10','S'); insert into tabs values(timestamp '2010-02-05 10:29:55','S'); insert into tabs values(timestamp '2010-02-05 10:30:12','C'); insert into tabs values(timestamp '2010-02-05 10:31:10','C'); insert into tabs values(timestamp '2010-02-05 10:33:15','S'); insert into tabs values(timestamp '2010-02-05 10:35:20','D'); insert into tabs values(timestamp '2010-02-05 10:37:19','D'); insert into tabs values(timestamp '2010-02-05 10:39:55','D'); with t as( select dt,status,row_number() over (order by status,dt)-row_number() over (order by dt,status) rid from tabs ) select dt,status from t t1 where not exists (select * from t where rid=t1.rid and dt<t1.dt) order by dt; drop table tabs; /* DT STATUS ------------------------- ------ 2010-02-05 10.25.10 S 2010-02-05 10.30.12 C 2010-02-05 10.33.15 S 2010-02-05 10.35.20 D */
------解决方案--------------------
--tt为你的表
select 日期 ,设备状态 from
(
select 日期,设备状态,
lag(设备状态,1) over(order by rownum) 设备状态1,
row_number() over(order by rownum) rn
from tt
)
where 设备状态1 <> 设备状态 or 设备状态1 is null;
------解决方案--------------------