I have a specific task using PostgreSQL, which i need to come with a SQL query, but unfortunately, i'm not able to get a right result.
The example of the table i have:
----------------------------------------------
| ID | value | usage-date |
----------------------------------------------
| 1 | value1 | 2020-09-10 |
----------------------------------------------
| 1 | value1 | 2020-09-15 |
----------------------------------------------
| 1 | value1 | 2020-09-20 |
----------------------------------------------
| 1 | value1 | 2020-09-23 |
----------------------------------------------
| 1 | value1 | 2020-09-25 |
----------------------------------------------
| 1 | value1 | 2020-09-30 |
----------------------------------------------
| 1 | value2 | 2020-09-15 |
----------------------------------------------
| 1 | value2 | 2020-09-20 |
----------------------------------------------
| 1 | value2 | 2020-09-23 |
----------------------------------------------
| 1 | value2 | 2020-09-25 |
----------------------------------------------
So the table is ordered by ID, value and usage-date. My task is to extract intervals that should tell from which date to which date a certain value was active. So the output would look like following:
--------------------------------------------------------------
| ID | value | start-date | end-date |
--------------------------------------------------------------
| 1 | value1 | 2020-09-10 | 2020-09-15 |
--------------------------------------------------------------
| 1 | value2 | 2020-09-15 | 2020-09-20 |
--------------------------------------------------------------
| 1 | value1 | 2020-09-20 | 2020-09-23 |
--------------------------------------------------------------
| 1 | value2 | 2020-09-23 | 2020-09-25 |
--------------------------------------------------------------
| 1 | value1 | 2020-09-25 | 2020-09-30 |
--------------------------------------------------------------
Anyone some idea how is it possible to do it?
Here is an SQL fiddle so anyone can try.