Detect consecutive dates ranges using SQL

后端 未结 7 1235
南笙
南笙 2020-11-28 07:54

I want to fill the calendar object which requires start and end date information. I have one column which contains a sequence of dates. Some of the dates are consecutive (ha

7条回答
  •  天命终不由人
    2020-11-28 08:28

    SELECT InfoDate ,
        CASE
          WHEN TRUNC(InfoDate - 1) = TRUNC(lag(InfoDate,1,InfoDate) over (order by InfoDate))
          THEN NULL
          ELSE InfoDate
        END STARTDATE,
        CASE
          WHEN TRUNC(InfoDate + 1) = TRUNC(lead(InfoDate,1,InfoDate) over (order by InfoDate))
          THEN NULL
          ELSE InfoDate
        END ENDDATE
      FROM TABLE;
    

提交回复
热议问题