Suppose I have a table like
| id |
|---|
| 1 |
| 3 |
| 4 |
| 10 |
| 12 |
| 19 |
and I'd like to group the ids (in sorted order) into the same group if they differ by 5 or less, and a new group if they differ by 6 or more. So the output would be:
| id | group |
|---|---|
| 1 | 1 |
| 3 | 1 |
| 4 | 1 |
| 10 | 2 |
| 12 | 2 |
| 19 | 3 |
Is this possible in SQL? It will be a query in Trino, and I see they have commands like lag and partition. Has anyone made a query like this that can help out?