我有一项任务是在Redshift中合并重叠的时间范围。
这是一个很好的解决方案:Merge overlapping time intervals, how?
不幸的是,Redshift不支持递归CTE。如何转换此代码以执行它而不递归?
WITH RECURSIVE cte( id, date_start, date_end ) AS
(
SELECT id, date_start, date_end
FROM evento
UNION
SELECT e.id,
least( c.date_start, e.date_start ),
greatest( c.date_end, e.date_end )
FROM cte c
JOIN evento e
ON e.date_start between c.date_start and c.date_end
OR
e.date_end between c.date_start and c.date_end
)
SELECT distinct date_start, date_end
FROM (
SELECT id,
min( date_start) date_start,
max( date_end ) date_end
FROM cte
GROUP BY id
) xx
ORDER BY date_start;
您可以将日期与之前的行日期进行比较,并使用累积总和构建细分ID,如下所示:
WITH
prev_dates as (
select id, date_start, date_end,
lag(date_end) over (order by date_start) as prev_date_end
from evento
),
sequences as (
select *,
sum(case when date_start>prev_date_end then 1 else 0 end) over (order by date_start) as sequence_id
from prev_dates
)
select
sequence_id,
min(date_start) as date_stat,
max(date_end) as date_end
from sequences
group by 1