Mysql
 sql >> Database >  >> RDS >> Mysql

Funzione Lead e Lag in MySQL

Puoi utilizzare correlato sottoquery:

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