Tema: Re: SQL
Autorius: 2x50
Data: 2010-04-15 23:23:09
....arba sitaip :)

with dummy_tbl as
(
select to_date('20100103', 'yyyymmdd') stamp from dual union all
select to_date('20100104', 'yyyymmdd') from dual union all
select to_date('20100105', 'yyyymmdd') from dual union all
select to_date('20100120', 'yyyymmdd') from dual union all
select to_date('20100121', 'yyyymmdd') from dual union all
select to_date('20100122', 'yyyymmdd') from dual union all
select to_date('20100123', 'yyyymmdd') from dual union all
select to_date('20100201', 'yyyymmdd') from dual union all
select to_date('20100202', 'yyyymmdd') from dual
)

select
group_id,
q2.stamp,
first_value (q2.stamp) over (partition by q2.group_id order by q2.stamp) 
start_date,
first_value (q2.stamp) over (partition by q2.group_id order by q2.stamp 
desc) end_date
from
(
select
  q1.stamp,
  sum (q1.start_of_group) over (order by q1.stamp) group_id
from
(
select
  t.stamp,
  lag (t.stamp, 1, t.stamp) over (order by t.stamp) pre_stamp,
  t.stamp - lag (t.stamp, 1, t.stamp-11) over (order by t.stamp) stamp_diff,
  case when t.stamp - lag (t.stamp, 1, t.stamp-11) over (order by t.stamp) > 
1 then 1 end start_of_group
from dummy_tbl t
) q1
) q2