Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Função Lead e Lag no Mysql


Você pode usar correlacionado subconsultas:
SELECT t1.case_id, t1.assigned_date,
       (SELECT t2.assigned_date
       FROM mytable AS t2
       WHERE t2.case_id = t1.case_id AND
             t2.assigned_date > t1.assigned_date
       ORDER BY t2.assigned_date LIMIT 1) AS next_date,
       (SELECT t2.assigned_date
       FROM mytable AS t2
       WHERE t2.case_id = t1.case_id AND
             t2.assigned_date < t1.assigned_date
       ORDER BY t2.assigned_date DESC LIMIT 1) AS prev_date
FROM mytable AS t1