Impala SQL: Merging rows with overlapping dates. WHERE EXISTS and recursive CTE not supported

若如初见. 提交于 2019-12-04 13:58:40
select  min(StartDate)  as StartDate
       ,max(EndDate)    as EndDate

from   (select  StartDate,EndDate
               ,count (is_gap) over
                (
                    order by    StartDate,ID
                )   as range_id

        from   (select  ID,StartDate,EndDate
                       ,case 
                            when    max (EndDate) over
                                    (
                                        order by    StartDate,ID
                                        rows        between unbounded preceding 
                                                    and     1 preceding
                                    ) < StartDate
                            then    true
                        end as is_gap

                from    t
                ) t
        ) t

group by    range_id

order by    StartDate
;

+------------+------------+
| startdate  | enddate    |
+------------+------------+
| 2017-01-01 | 2017-04-01 |
| 2017-05-05 | 2017-08-31 |
| 2017-10-01 | 2017-10-05 |
| 2017-11-01 | 2017-12-25 |
+------------+------------+
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!