I am using Teradata SQL and have the following data:
ID | MonthID | Acc | Complaint |
---|---|---|---|
1 | 202202 | 5 | 1 |
1 | 202203 | 4 | 2 |
1 | 202204 | 3 | 0 |
2 | 202202 | 2 | 0 |
2 | 202203 | 3 | 1 |
2 | 202204 | 2 | 3 |
3 | 202202 | 1 | 0 |
3 | 202203 | 2 | 0 |
3 | 202204 | 3 | 0 |
I want to retrieve all the rows after per ID ever since when Complaint is > 0 ordered by ID, MonthID.
Expected Output:
ID | MonthID | Acc | Complaint |
---|---|---|---|
1 | 202202 | 5 | 1 |
1 | 202203 | 4 | 2 |
1 | 202204 | 3 | 0 |
2 | 202203 | 3 | 1 |
2 | 202204 | 2 | 3 |
Can someone please help?
Thanks!
Last attempt code:
select a.*,
row_number() over (partition by ID order by Complaint, MonthID)
from table a