MySQL Sequence Group

I have a tariff table, with data for each employee and changes in his rates in the workplace. I want each employee’s job change to be done in one request. The selected job is the latest update in sequential lines. Please note that the same position may be assigned to an employee over time.

id job_id updated
1    1    01-01-2015
2    1    01-02-2015
3    2    01-01-2015
4    2    01-03-2015
5    2    01-02-2015
6    1    01-01-2015

the result should be:

id job_id updated
1    1    01-01-2015
3    2    01-01-2015
6    1    01-02-2015
+1
source share
1 answer

ok, here is my script link based on my understanding of the output you provided,

( mysql, ), , job , .

select t1.id
     , t1.job_id
     , t1.updatedate
     , t2.rn as rnt2
  from temprwithrn as t1
  left 
  join temprwithrn as t2
    on t1.rn = t2.rn + 1
 where t1.job_id <> t2.job_id or t2.job_id is null
+3

Source: https://habr.com/ru/post/1667746/


All Articles