我在Redshift数据库中有一个表,其中包含分组的可能重叠的间隔,如下所示:
| interval_id | l | u | group |
| ----------- | -- | -- | ----- |
| 1 | 1 | 10 | A |
| 2 | 2 | 5 | A |
| 3 | 5 | 15 | A |
| 4 | 26 | 30 | B |
| 5 | 28 | 35 | B |
| 6 | 30 | 31 | B |
| 7 | 44 | 45 | B |
| 8 | 56 | 58 | C |我想要做的是确定组内区间并集的长度。也就是说,对于每个间隔,取u - l,对所有组成员求和,然后减去间隔之间重叠的长度。
期望的结果:
| group | length |
| ----- | ------ |
| A | 14 |
| B | 10 |
| C | 2 |这个问题has been asked before,似乎该线程中的所有解决方案都使用了Redshift不支持的功能。
发布于 2017-03-07 06:16:49
这并不困难,但需要多个步骤。关键是定义每个组中的“岛”,然后在这些岛上聚合。大量的子查询、聚合和窗口函数。
select groupId, sum(ul)
from (select groupId, (max(u) - min(l) + 1) as ul
from (select t.*,
sum(case when prev_max_u < l then 1 else 0 end) over (order by l) as grp
from (select t.*,
max(u) over (order by l rows between unbounded preceding and 1 preceding) as prev_max_u
from t
) t
) t
group by groupid, grp
) g
group by groupId;这个想法是为了确定在每条记录的开头是否有重叠。为此,它对所有前面的记录使用累积最大值函数。然后,它通过将前一个最大值与当前l进行比较来确定是否存在重叠-重叠的累积总和定义了一个组。
其余的只是聚合。和更多的聚合。
https://stackoverflow.com/questions/42636540
复制相似问题