H.jalali
H.jalali

Reputation: 119

How get last row with different value in some columns in mariaDB

I need get last rows (for example 10 rows) With different values in some columns (for example in cat column ) in mariaDB.

id cat name
1 val1 name1
2 val1 name2
3 val2 name3
4 val2 name4
5 val2 name5
6 val3 name6
7 val3 name7
8 val3 name8

result :

id cat name
2 val1 name2
5 val2 name5
8 val3 name8

Upvotes: 0

Views: 389

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

You can use lead():

select t.*
from (select t.*,
             lead(cat) over (order by id) as next_cat
      from t
     ) t
where next_cat is null or next_cat <> cat;

Here is a db<>fiddle.

Upvotes: 1

Related Questions