Mysql
 sql >> Base de données >  >> RDS >> Mysql

Fonction Lead et Lag dans Mysql

Vous pouvez utiliser corrélé sous-requêtes :

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