Let's assume I have a table "TABLE_A" in an Oracle database:
=======================
| id | key | date |
=======================
| 0 | 1 | 1.1.2020 |
| 1 | 1 | 1.1.2021 |
=======================
I want to get a result like this:
===================================
| id | key | date | next_date |
===================================
| 0 | 1 | 1.1.2020 | 1.1.2021 |
===================================
Note that I want the row with a certain key <ID> on a certain date <DATE> with another column that contains the next date in the database with the same key. However if there is no other date it should still give me the same row but with next_date empty.
Is there a simpler / better / more readable version than this?
SELECT a.*, next_date
FROM TABLE_A a,
(SELECT key, date as next_date
FROM TABLE_A
WHERE key = <ID>
AND date > <DATE>
AND ROWNUM <= 1
ORDER BY next_date asc) a2
WHERE key = <ID>
AND date = <DATE>
AND a2.key(+) = a.key
