I have some data in database:
id user 1 zhangsan 2 zhangsan 3 zhangsan 4 lisi 5 lisi 6 lisi 7 zhangsan 8 zhangsan
I want keep order, a
if in sql server, oracle, db2...
with x as( select c.*, rn = row_number() over (order by c.id) from test c left join test n on c.[user] = n.[user] and c.[id] + 1 = n.[id] where n.id is null ) select a.[user], a.id - coalesce(b.id, 0) from x a left join x b on a.rn = b.rn + 1