sc1234
sc1234

Reputation: 147

Group rows with the same value only if they are one after the other in MySQL

I want to achieve something very similar to this but using MySQL:

Group rows if certain column value directly after another in SQL

However, in my case the id is not incremented by 1 but there is a random gap so for instance:

0 -- bob -- note -- 14:00
3 -- bob -- note -- 15:00
9 -- bob -- time -- 15:00
20 - bob -- note -- 15:00

I need to display:
0 -- bob -- note -- 14:00
9 -- bob -- time -- 15:00
20 - bob -- note -- 15:00

Any help would be greatly appreciated :)

Upvotes: 1

Views: 344

Answers (1)

splash58
splash58

Reputation: 26143

select id,  Name,  Event,  Time 
  from (select *, @e prev, @e:=Event 
          from table, (select @e:='') e) t 
  where prev<>Event

Demo on sqlfiddle

Upvotes: 2

Related Questions