How would I select the row that is the first occurrence in the last 'grouping' of consecutive rows, where a grouping is defined by the consecutive appearance of a particular column value (in the example below state).
For example, given the following table:
| id | datetime | state | value_needed |
|---|---|---|---|
| 1 | 2021-04-01 09:42:41.319000 | incomplete | A |
| 2 | 2021-04-04 09:42:41.319000 | done | B |
| 3 | 2021-04-05 09:42:41.319000 | incomplete | C |
| 4 | 2021-04-05 10:42:41.319000 | incomplete | C |
| 5 | 2021-04-07 09:42:41.319000 | done | D |
| 6 | 2021-04-012 09:42:41.319000 | done | E |
I would want the row with id=5 as it it is the first occurrence of state=done in the last (i.e. most recent) grouping of state=done.