I have a question quite similar to How to add sequence number for groups in a SQL query without temp tables
To take the sample from there, I have a data-structure that consists of the first two columns of this table, and I want to generate the third column in my query:
Record Group GroupSequence -------|---------|-------------- 1 Chickens 1 2 Chickens 1 3 Horses 2 4 Cows 3 5 Horses 4 6 Horses 4
The difference to the quoted query is, that I need a new number if the previous line didn't have the same value in the second column.
I tried adding a row-number and using LAG()
to check if the previous line had the same value - if so, take the previous value of this generated column, if not, take the row-number - but it looks like you can't reuse the column you're building.
The value just needs to be a different number - it doesn't matter if it's in order. This would also be fine:
Record Group GroupSequence -------|---------|-------------- 1 Chickens 1 2 Chickens 1 3 Horses 3 4 Cows 4 5 Horses 5 6 Horses 5