Suppose we have below table:
| Code | Dt |
|---|---|
| c1 | 2020-10-01 |
| c1 | 2020-10-05 |
| c1 | 2020-10-09 |
| c1 | 2020-10-10 |
| c1 | 2020-10-20 |
| c2 | 2020-10-07 |
| c2 | 2020-10-09 |
| c2 | 2020-10-15 |
| c2 | 2020-10-16 |
| c2 | 2020-10-20 |
| c2 | 2020-10-24 |
Combination of Code and Dt is unique. Rows are sorted based on Code and Dt. Database is Oracle 12. For every code, I want to get list of its Dts that each Dt is grater than 7 days compared to previously selected Dt. Therefore, result should be:
| Code | Dt |
|---|---|
| c1 | 2020-10-01 |
| c1 | 2020-10-09 |
| c1 | 2020-10-20 |
| c2 | 2020-10-07 |
| c2 | 2020-10-15 |
| c2 | 2020-10-24 |
I've tried self join based on row_number() to join every row with its previous row if date difference is grater than 7. But there is a challenge that each row should be compared with previously selected row and not its previous row in table. Any solutions? Thanks