I have a simple table and I need to identify groups of four rows (groups are not goals), but each row of each row has a value of +1. For instance:
----------------------
| language | id |
----------------------
| C | 16 |
| C ++ | 17 |
| Java | 18 |
| Python | 19 |
| HTML | 65 |
| Javascript | 66 |
| Php | 67 |
| Perl | 68 |
----------------------
I want to add a column that indicates a group or set, how can I get this output using MySQL ?:
----------------------------
| language | id | set |
----------------------------
| C | 16 | 1 |
| C ++ | 17 | 1 |
| Java | 18 | 1 |
| Python | 19 | 1 |
| HTML | 65 | 2 |
| Javascript | 66 | 2 |
| Php | 67 | 2 |
| Perl | 68 | 2 |
----------------------------
Please note that in this example there are only 2 sets (this can be 1 or more sets), and they did not start with 16 (such values ββare not known, but the restriction is that each id value of each row has this form n, n + 1, n + 2 and n + 3).
I studied the Gaps and Islands problem, but did not understand how to solve it using their solutions. I am also looking through stackoverflow, but the closest question I found is How to find spaces in sequential numbering in mysql?
thanks
source share