oracle query to loop through multiple records and provide the results
Please help qrite an oracle query to achieve above results.
input data
| ID | ABRV | NXTFILL | DAYSOUT_INT | DAYSOVER_INT | TIME |
|---|---|---|---|---|---|
| 123 | abc | null | null | null | 21 |
| 123 | abc | 15 | 14 | 0 | 6 |
| 123 | abc | 2 | 1 | 0 | 4 |
| 123 | abc | 4 | 0 | 27 | 0 |
| 456 | xyz | null | null | null | 4 |
| 456 | xyz | 2 | 1 | 0 | 2 |
| 456 | xyz | 2 | 1 | 0 | 0 |
| 888 | pqr | null | null | null | 10 |
| 888 | pqr | 10 | 9 | 0 | 0 |
| 888 | def | null | null | null | 4 |
| 888 | def | 4 | 3 | 0 | 0 |
| 999 | ttt | null | null | null | 23 |
| 999 | ttt | 23 | 0 | 7 | 0 |
| 999 | sss | null | null | null | 25 |
| 999 | sss | 25 | 0 | 5 | 0 |
| 999 | rrr | null | null | null | 20 |
| 999 | rrr | 20 | 0 | 10 | 0 |
| 444 | ddd | null | null | null | 3 |
| 444 | ddd | 3 | 0 | 87 | 0 |
| 444 | eee | null | null | null | 3 |
| 444 | eee | 3 | 0 | 87 | 0 |
expected output data
| ID | ABRV | NXTFILL | DAYSOUT_INT | DAYSOVER_INT | TIME | DAYSOVERX | daysout_sk | daysover_ck |
|---|---|---|---|---|---|---|---|---|
| 123 | abc | null | null | null | 21 | 0 | null | null |
| 123 | abc | 15 | 14 | 0 | 6 | 12 | 0 | 12 |
| 123 | abc | 2 | 1 | 0 | 4 | 26 | 0 | 26 |
| 123 | abc | 4 | 0 | 27 | 0 | 27 | 0 | 27 |
| 456 | xyz | null | null | null | 4 | 0 | null | null |
| 456 | xyz | 2 | 1 | 0 | 2 | 0 | 1 | 0 |
| 456 | xyz | 2 | 1 | 0 | 0 | 0 | 1 | 0 |
| 888 | pqr | null | null | null | 10 | 0 | null | null |
| 888 | pqr | 10 | 9 | 0 | 0 | 0 | 9 | 0 |
| 888 | def | null | null | null | 4 | 0 | null | null |
| 888 | def | 4 | 3 | 0 | 0 | 0 | 3 | 0 |
| 999 | ttt | null | null | null | 23 | 0 | null | null |
| 999 | ttt | 23 | 0 | 7 | 0 | 7 | 0 | 7 |
| 999 | sss | null | null | null | 25 | 0 | null | null |
| 999 | sss | 25 | 0 | 5 | 0 | 5 | 0 | 5 |
| 999 | rrr | null | null | null | 20 | 0 | null | null |
| 999 | rrr | 20 | 0 | 10 | 0 | 10 | 0 | 10 |
| 444 | ddd | null | null | null | 3 | 0 | null | null |
| 444 | ddd | 3 | 0 | 87 | 0 | 87 | 0 | 87 |
| 444 | eee | null | null | null | 3 | 0 | null | null |
| 444 | eee | 3 | 0 | 87 | 0 | 87 | 0 | 87 |
Please help write an oracle query to achieve above results.
My logic is as follows-
SELECT id, ABRV, NXTFILL, DAYSOUT_INT, DAYSOVER_INT, TIME, CASE WHEN DAYSOVER_INT IS NOT NULL THEN DAYSOVER_INT ELSE 0 END AS DAYSOVERX, SUM(DAYSOUT_INT) OVER (PARTITION BY id ORDER BY TIME ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS daysout_sk, SUM(DAYSOVER_INT) OVER (PARTITION BY id ORDER BY TIME ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS daysover_ck FROM test1 where ORDER BY ID, TIME; not working as expected.
DAYSOVERX? Please elaborate.