Detect and merge date range successive overlaps in SQL

Anirudh

I have a need to detect and combine overlapping date ranges in a table but only in successive rows, non-succesive overlaps are to be ignored.

CREATE TABLE konto (konto_nummer     INTEGER, start_datum DATE, end_datum DATE); 
INSERT INTO konto VALUES (1,   '2020-01-01 00:00:00.000000', '2020-01-10 00:00:00.000000');
INSERT INTO konto VALUES (1,   '2020-01-12 00:00:00.000000',    '2020-01-20 00:00:00.000000');
INSERT INTO konto VALUES (2,    '2020-01-01 00:00:00.000000',   '2020-01-10 00:00:00.000000');
INSERT INTO konto VALUES (2,    '2020-01-05 00:00:00.000000',   '2020-01-20 00:00:00.000000');
INSERT INTO konto VALUES (2,    '2020-01-15 00:00:00.000000',   '2020-01-25 00:00:00.000000');
INSERT INTO konto VALUES (2,    '2020-02-05 00:00:00.000000',   '2020-02-20 00:00:00.000000');
INSERT INTO konto VALUES (3,    '2020-01-01 00:00:00.000000',   '2020-01-25 00:00:00.000000');
INSERT INTO konto VALUES (4,    '2020-04-01 00:00:00.000000',   '2020-04-10 00:00:00.000000');
INSERT INTO konto VALUES (4,    '2020-04-05 00:00:00.000000',   '2020-04-15 00:00:00.000000');
INSERT INTO konto VALUES (4,    '2020-04-16 00:00:00.000000',   '2020-04-25 00:00:00.000000');
INSERT INTO konto VALUES (4,    '2020-04-20 00:00:00.000000',   '2020-04-30 00:00:00.000000');

The same colored rows are having successive overlaps.

I tried the following

    SELECT
    ROW_NUMBER () OVER (ORDER BY konto_nummer, start_datum, end_datum) AS RN,
    konto_nummer,
    start_datum,
    end_datum,
    MAX(end_datum) OVER (PARTITION BY konto_nummer ORDER BY start_datum, end_datum ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS Previousend_datum
   FROM konto;

But it combines also non-consecutive overlaps.

Mike Organek

Gaps and Islands has multiple steps.

First, mark the gaps

with mark as (
  select *, 
         lag(end_datum) over w
           not between start_datum and end_datum as island
    from konto
  window w as (partition by konto_nummer
                   order by start_datum, end_datum)
),

Then, number the islands

 grps as (
  select *, 
         sum(coalesce(island, true)::int) over w as grpnum 
    from mark
  window w as (partition by konto_nummer
                   order by start_datum, end_datum)
)

Then aggregate by group

select konto_nummer, 
       min(start_datum) as start_datum, 
       max(end_datum) as end_datum
  from grps
 group by konto_nummer, grpnum
 order by 1, 2, 3;

Working fiddle here.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL - How to declare date range in a temp table?

分類Dev

SQL find count within date range

分類Dev

SQL: Get users not loggedIn in given date range

分類Dev

SQL Server - find people between date range excluding the year

分類Dev

SQL Input Date Range to work out Rate per day

分類Dev

SQL Create dates between date range for multiple dimensions

分類Dev

SQL: find records having data for each month in a given date range

分類Dev

PL/SQL function to calculate average monthwise for a given date range

分類Dev

SQL Server - Deleting rows between a date range using SQL. Date conversion fails

分類Dev

epplus range merge AutoFilter

分類Dev

SQL Find Where Date Falls in Table Based on Date Range in Another Table

分類Dev

T-SQL date range in a table split and add the individual date to the table

分類Dev

SQL Server: Adjusting queried date range based on current date and adjust column headers as well

分類Dev

pandas: merge conditional on time range

分類Dev

xlsxwirter merge_range with URL

分類Dev

Mongoengine query with date range

分類Dev

Return date range by group

分類Dev

Elasticsearch date range issue

分類Dev

Date range selector for barchart

分類Dev

Is it possible to schedule queries in Google BigQuery using standard SQL such that date range increments everyday while it runs?

分類Dev

MS Access VBA SQL SELECT * INTO tempTbl WHERE Stuff = "" AND OtherStuff BETWEEN Date Range

分類Dev

Get a random date within a date range

分類Dev

Loop through a date range with JavaScript

分類Dev

Limit date range in Joda Time

分類Dev

jQuery DataTables Date Range Styling

分類Dev

Limit date range in Joda Time

分類Dev

Elasticsearch count in groups by date range

分類Dev

Lambda join between date range

分類Dev

How to find field with date range?

Related 関連記事

  1. 1

    SQL - How to declare date range in a temp table?

  2. 2

    SQL find count within date range

  3. 3

    SQL: Get users not loggedIn in given date range

  4. 4

    SQL Server - find people between date range excluding the year

  5. 5

    SQL Input Date Range to work out Rate per day

  6. 6

    SQL Create dates between date range for multiple dimensions

  7. 7

    SQL: find records having data for each month in a given date range

  8. 8

    PL/SQL function to calculate average monthwise for a given date range

  9. 9

    SQL Server - Deleting rows between a date range using SQL. Date conversion fails

  10. 10

    epplus range merge AutoFilter

  11. 11

    SQL Find Where Date Falls in Table Based on Date Range in Another Table

  12. 12

    T-SQL date range in a table split and add the individual date to the table

  13. 13

    SQL Server: Adjusting queried date range based on current date and adjust column headers as well

  14. 14

    pandas: merge conditional on time range

  15. 15

    xlsxwirter merge_range with URL

  16. 16

    Mongoengine query with date range

  17. 17

    Return date range by group

  18. 18

    Elasticsearch date range issue

  19. 19

    Date range selector for barchart

  20. 20

    Is it possible to schedule queries in Google BigQuery using standard SQL such that date range increments everyday while it runs?

  21. 21

    MS Access VBA SQL SELECT * INTO tempTbl WHERE Stuff = "" AND OtherStuff BETWEEN Date Range

  22. 22

    Get a random date within a date range

  23. 23

    Loop through a date range with JavaScript

  24. 24

    Limit date range in Joda Time

  25. 25

    jQuery DataTables Date Range Styling

  26. 26

    Limit date range in Joda Time

  27. 27

    Elasticsearch count in groups by date range

  28. 28

    Lambda join between date range

  29. 29

    How to find field with date range?

ホットタグ

アーカイブ