with w as (
select 1 id, to_date('1/1/2012', 'mm/dd/yyyy') db, to_date('1/2/2012', 'mm/dd/yyyy') de, 500 s from dual
union all
select 1, to_date('1/2/2012', 'mm/dd/yyyy'), to_date('1/3/2012', 'mm/dd/yyyy'), 500 from dual
union all
select 1, to_date('1/3/2012', 'mm/dd/yyyy'), to_date('1/4/2012', 'mm/dd/yyyy'), 1000 from dual
union all
select 1, to_date('1/4/2012', 'mm/dd/yyyy'), to_date('1/5/2012', 'mm/dd/yyyy'), 750 from dual
union all
select 1, to_date('1/5/2012', 'mm/dd/yyyy'), to_date('1/6/2012', 'mm/dd/yyyy'), 750 from dual
union all
select 1, to_date('1/6/2012', 'mm/dd/yyyy'), to_date('1/7/2012', 'mm/dd/yyyy'), 500 from dual
union all
select 1, to_date('1/7/2012', 'mm/dd/yyyy'), to_date('1/8/2012', 'mm/dd/yyyy'), 500 from dual
union all
select 1, to_date('1/8/2012', 'mm/dd/yyyy'), to_date('1/9/2012', 'mm/dd/yyyy'), 510 from dual
)
select tmin.db, tmax.de, tmin.s
from
(
select
row_number() over (order by db) id,
db,
s
from
(
select
db,
s,
case
when ps is null
then 1
when ps != s
then row_number() over (order by db)
else 0 end num
from (
select
(db)
, (de)
, lag (s,1) over (ORDER BY db) ps
, s
from w
) t
) t1
where num != 0
) tmin,
(select
row_number() over (order by db) id,
de,
s
from
(
select
db,
de,
s,
case
when ps is null
then 1
when ps != s
then row_number() over (order by de desc)
else 0 end num
from (
select
db
,(de)
, lag (s,1) over (ORDER BY de desc) ps
, s
from w
order by db
) t
) t1
where num != 0) tmax
where tmin.id = tmax.id