Home > Software design >  How can I get consecutive dates in one row using SQL?
How can I get consecutive dates in one row using SQL?

Time:01-18

I want to get starttime minimum and endtime maximum on consecutive dates(must be the same month) , but all dates have to be consecutive.

I want to combine them in one row. How can I get the following table?(Table 2)

IP Address Starttime Endtime
192.168. 0.1 15/12/2022 15/12/2022
192.168. 0.1 26/12/2022 26/12/2022
192.168. 0.1 27/12/2022 27/12/2022
192.168. 0.1 28/12/2022 28/12/2022
192.168. 0.1 11/01/2023 11/01/2023
192.168. 0.1 12/01/2023 12/01/2023
192.168. 0.1 13/01/2023 13/01/2023
192.168. 0.1 14/01/2023 14/01/2023
192.168. 0.1 15/01/2023 15/01/2023

This is actually what I want:

IP Address Starttime Endtime
192.168. 0.1 15/12/2022 15/12/2022
192.168. 0.1 26/12/2022 28/12/2022
192.168. 0.1 11/01/2023 15/01/2023

CodePudding user response:

Use LAG(endtime) or LEAD(starttime) OVER (PARTITION BY ip_address ORDER BY starttime) on a first pass so that each row can see the date from its neighboring row ( you can go in either direction). Then use a CASE or DECODE to test whether the dates are contiguous or not. If they are not, then emit the date, else emit NULL. Then in a parent block, you can use LAST_VALUE() with IGNORE NULLS option and ROWS BETWEEN UNBOUNDED PRECEDING TO CURRENT ROW to get the most recent of those conditionally computed dates. That can then be used in a GROUP BY in yet a third parent query block to finalize your results.

CodePudding user response:

Or the classical MATCH_RECOGNIZE for the merging of intervals:

select * from data 
match_recognize(
    partition by ipaddress
    order by starttime, endtime
    measures first(starttime) as starttime, max(endtime) as endtime
    pattern( merged* strt )
    define
        merged as endtime = next(starttime) - 1 
);
  • Related