Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Lead- und Lag-Funktion in Mysql

Sie können korreliert verwenden Unterabfragen:

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