oracle 合并连续时间sql

http://stackoverflow.com/questions/427448/grouping-sql-results-by-continous-time-intervals-oracle-sql

select id, min(start_date) period_start, max(end_date) period_end
from
(
select 
    id, start_date, end_date,
    max(contig) over (partition by id order by end_date) contiguous_group
from
(
select 
    id, start_date, end_date,
    case 
        when lag(end_date) over (partition by id order by end_date) != start_date-1 or row_number() over (partition by id order by end_date)=1 
            then row_number() over (partition by id order by end_date) else null end contig
from t2
)
)
group by id, contiguous_group
order by id, period_start

猜你喜欢

转载自wjhu.iteye.com/blog/1591125
今日推荐