I am trying to aggregate overlapping effective dates. Any gaps between dates should be considered as separate rows. I am using min and max and I am getting below output but would like to see expected output.
My query
WITH test_data AS (
SELECT '2020-01-01' AS date_from,
'2020-01-03' AS date_to,
'1' AS product
UNION ALL
SELECT '2020-01-05' AS date_from,
'2020-01-07' AS date_to,
'1' AS product
UNION ALL
SELECT '2020-01-06' AS date_from,
'2020-01-10' AS date_to,
'1' AS product
)
SELECT product,
MIN(date_from) AS date_from,
MAX(date_to) AS date_to
FROM test_data
GROUP BY 1;
Source data
date_from | date_to | product |
---|---|---|
2020-01-01 | 2020-01-03 | 1 |
2020-01-05 | 2020-01-07 | 1 |
2020-01-06 | 2020-01-10 | 1 |
Output table
date_from | date_to | product |
---|---|---|
2020-01-01 | 2020-01-10 | 1 |
Expected output
date_from | date_to | product |
---|---|---|
2020-01-01 | 2020-01-03 | 1 |
2020-01-05 | 2020-01-10 | 1 |
Thanks in advance !