Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: SQL puzzle
ok, this one will do it (8.1.6 and above):
SELECT * FROM (
SELECT device,
nvl(begin_time, lag(begin_time) over (ORDER BY r)) AS begin_time, end_time, end_flag, status
device, TIMESTAMP, status, voriger_status, naechster_status, naechster_timestamp, CASE WHEN status != nvl(voriger_status,'_') THEN TIMESTAMP ELSE null END AS begin_time, CASE WHEN naechster_status IS NULL THEN null WHEN status != naechster_status THEN naechster_timestamp ELSE null END AS end_time, CASE WHEN naechster_status IS NULL THEN 'y' WHEN status != naechster_status THEN 'y' ELSE null END AS end_flag
TIMESTAMP, status, lag(status) over(PARTITION BY device ORDER BY timestamp) voriger_status, lead(status) over(PARTITION BY device ORDER BY timestamp) naechster_status, lead(timestamp) over(PARTITION BY device ORDER BY timestamp) naechster_timestamp, rownum r
end_time IS NOT NULL
) v
WHERE end_flag = 'y'
/
regards
Marc Blum
mailto:marc_at_marcblum.de
http://www.marcblum.de
Received on Sat Feb 16 2002 - 14:54:04 CST