繁体   English   中英

确定日期范围并合并为最大和最小日期

[英]Identify date range and merge into max and min dates

我有数据(int、date、date 类型)

SELECT * FROM 
(
  VALUES 
     (1700171048,'2020-12-21','2021-01-03'), 
     (1700171048,'2021-01-05','2021-01-12'), 
     (1700171048,'2021-01-13','2021-01-17'), 
     (1700171048,'2021-01-18','2021-01-19'),
     (1700171048,'2021-01-22','2021-01-27'),
     (1700171048,'2021-01-28','2021-02-17')
     
     (1700171049,'2020-12-21','2021-01-03'), 
     (1700171049,'2021-01-04','2021-01-05'), 
     (1700171049,'2021-01-06','2021-01-17'),
     (1700171049,'2021-01-18','2021-01-19'),
     (1700171049,'2021-01-20','2021-01-27'),
     (1700171049,'2021-01-28','2021-02-17')

 ) AS c (id1, st, endt )

我需要输出(即,如果开始日期和结束日期是连续的,则使其成为组的一部分)

 id1         st             endt
 1700171048 '2020-12-21' , '2021-01-03'
 1700171048 '2021-01-05' , '2021-01-19'
 1700171048 '2021-01-22' , '2021-02-17'

 1700171049 '2020-12-21' to '2021-02-17'

这个我试过了,不行。

    select id, case when min(b.st) = max(b.endt) + 1 then min(b.st) end,
            case when min(b.endt) = min(b.st) + 1 then max(b.st) end 
     from c a  join c b
   group by id 

这是一种差距和孤岛问题。 使用lag()来确定是否存在重叠。 然后是没有重叠和聚合时的累积和:

select id1, min(st), max(endt)
from (select t.*,
             sum(case when prev_endt >= st + interval '-1 day' then 0 else 1 end) over (partition by id1 order by st) as grp
      from (select t.*,
                   lag(endt) over (partition by id1 order by st) as prev_endt
            from t
           ) t
     ) t
group by id1, grp;

是一个 db<>fiddle。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM