So, I have the record in my database:
id | Date Time | Name | level |
---|---|---|---|
0 | 2022-03-22 23:18:01 | John | 615 |
1 | 2022-03-22 23:17:01 | John | 616 |
2 | 2022-03-22 23:16:02 | John | 616 |
3 | 2022-03-22 23:15:01 | John | 616 |
4 | 2022-03-22 23:14:01 | John | 617 |
5 | 2022-03-22 23:13:01 | John | 617 |
6 | 2022-03-22 23:12:01 | John | 616 |
7 | 2022-03-22 23:11:01 | John | 617 |
8 | 2022-03-22 23:10:02 | John | 618 |
9 | 2022-03-22 23:09:01 | John | 618 |
As a result I want to get these values where the next "lvl" is different from the previous "lvl". I mean, I want to get the result record with id:
0 - Because it's the first one,
1 - Because lvl is not equal to 0,
2,3 - skipped because lvl is the same as 1
4 - Because lvl is different from 1,
5 - skipped because lvl is the same as 4,
6 - Because the level is different from 5,
7 - Because the level is different from 6,
8 - Because the level is different from 7,
9 - Skip.
P粉1649427912024-04-05 16:08:06
This seems to be a good fit for lag
analytic functions (using MySQL 8):
with keep as ( select *, if(id=Min(id) over() or level != lag(level) over(order by id), 1, 0) keepme from t ) select id from keep where keepme=1;