Let's say I have a table that contains information about user savings to a thrift society and the assessment of each deposit type as good or bad based on internal logic. How do I select rows from this table so that all preceding rows to the last good row are skipped per user?
Before
id | user |type | amount ---------------------------- 20 | 98 | good | 40 35 | 98 | bad | 30 62 | 98 | good | 20 89 | 98 | bad | 60 93 | 98 | bad | 10 100 | 99 | good | 20 103 | 99 | good | 22 109 | 99 | good | 220 121 | 99 | bad | 640 193 | 99 | bad | 110
I would like to ignore all records for a user until the last good row is encountered, then subsequent rows can be counted. The rows are ordered by increasing ids which are not consecutive.
After
id | user |type | amount ---------------------------- 62 | 98 | good | 20 89 | 98 | bad | 60 93 | 98 | bad | 10 100 | 99 | good | 220 121 | 99 | bad | 640 193 | 99 | bad | 110