I have a table containing the following data, using Postgres 9.6:
log_id | sequence | made_at (timestamp) 206480 1 1 206480 1 2 206480 2 3 206480 3 4 206480 1 5 206480 2 6 206480 4 7 206480 5 8 206480 1 9 206480 2 10 206481 1 11 206481 2 12 206481 3 13 206481 4 14
I have to group and aggregate on the ID so I get an array of possible sequences. In the end I want the data to look like this:
log_id | sequence 206480 {1,1,2,3} 206480 {1,2,4,5} 206480 {1,2} 206481 {1,2,3,4}
I want a new row (with the sequences) when:
- the
log_id
changes; or - the next sequence number is lower than the current sequence number.
There is another column which specifies the ordering (a timestamp), but it's in another table (I join them and use that timestamp). I left it out to make things easier, but we can assume the column is called made_at
.