I'm trying and googling since a few hours, but got no solution that works. Here is my issue:
I have a table with following structure: id SERIAL, datum DATE, otherinfo VARCHAR(50)
The data could have equal date entries:
id datum otherinfo
1 2019-12-28 testdata1
2 2019-12-28 testdata2
3 2019-12-29 testdata3
4 2019-12-29 testdata4
5 2019-12-31 testdata5-begin longest consecutive days
6 2019-12-31 testdata6
7 2020-01-01 testdata7
8 2020-01-01 testdata8
9 2020-01-02 testdata9
10 2020-01-03 testdata10
11 2020-01-04 testdata11
12 2020-01-04 testdata12
13 2020-01-05 testdata13-end longest consecutive days
14 2020-01-22 testdata14
15 2020-01-29 testdata15
16 2020-01-30 testdata16
I'm interested to get the consecutive days with the start and end date. An output like this:
count | date MIN | date MAX
6 2019-12-31 2020-01-05
2 2019-12-28 2019-12-29
2 2020-01-29 2020-01-30
I found a few ideas how to solve here on Stackoverflow, but it seems that it always conflicts with multiple equal date entries.
Most success I had with following SQL query:
SELECT COUNT(*) -1 "count", MAX(datum), MIN(datum) FROM (SELECT *, date(datum) - row_number() OVER (PARTITION BY datum ORDER BY date(datum)) * INTERVAL '1 day' "filter" FROM table ) t1 GROUP BY filter HAVING COUNT(*) -1 > 0 ORDER BY count DESC
Sadly it gives wrong counts of consecutive days and the counted days even don't match with the start / end dates.
Many thanks for ideas
Martin