If I have a table defined like this in a postgres 12.4 database
id, name, age, enters, exits
Within that table there can be mutliple entries with the same name.
I want to create a select statement that introduces a group_id column to the results. The group_id will increment based on the value of the exits column. If it contains null then the group_id needs to increment and subsequent rows will belong to the new group_id until the next exits=null is encountered.
For example if the info table contains:
id, name, age, enters, exits
1, orange, 10, null, 8
2, orange, 8, 3, 5
3, orange, 4, 9, null
4, orange, 11, null, 5
5, orange, 3, 3, null
6, lemon, 9, 1, 2
Then a select * type query would return this:
id, group_id, name, age, enters, exits
1, 1, orange, 10, null, 8
2, 1, orange, 8, 3, 5
3, 1, orange, 4, 9, null
4, 2, orange, 11, null, 5
5, 2, orange, 3, 3, null
6, 3, lemon, 9, 1, 2
I am relatively new to SQL and after lots of searches and attempts I haven't made any progress. Most of the examples out there are much more complicated than this and I don't understand enough to deconstruct them into something that works.
Any help or pointers appreciated.