I have a table:
Status | Date |
---|---|
ok | 2023-05-01 00:00:00 |
ok | 2023-05-01 00:01:00 |
ok | 2023-05-01 00:02:00 |
no | 2023-05-01 00:03:00 |
no | 2023-05-01 00:04:00 |
ok | 2023-05-01 00:04:00 |
ok | 2023-05-01 00:05:00 |
I want to stash statuses coming having the same value and coming in a row and save status, start, end. So the above table would be transformed into:
Status | Start | End |
---|---|---|
ok | 2023-05-01 00:00:00 | 2023-05-01 00:02:00 |
no | 2023-05-01 00:03:00 | 2023-05-01 00:04:00 |
ok | 2023-05-01 00:04:00 | 2023-05-01 00:05:00 |
How can I do this using PostgreSQL?
2
Answers
One way of dealing with this problem is:
Output:
Check the demo here.
You can use something like:
Fiddle: https://www.db-fiddle.com/f/o5v9CtBaQZuXsC4XaSB1Cv/0