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

MySQL-Datetime-Vergleich mit vorheriger Zeile

Dies kann durch sequentielles Scannen der Tabelle und die Verwendung von MySQL-Variablen gehandhabt werden. Sie können testen in (aktualisiert ) SQL-Geige :

SELECT date2
     , dd 
     , DATE_FORMAT(dd, '%b %e, %Y') AS final_date
     , date1_duplicate
     , date1_was_null
FROM
( SELECT date2
       , COALESCE( (date1 = @d OR date1 = @prev), FALSE)
           AS date1_duplicate
       , (date1 IS NULL)               AS date1_was_null
       , @d := CASE WHEN (date1 = @d OR date1 = @prev) 
                 THEN date2 
                 ELSE COALESCE(date1, date2) 
               END AS dd
       , @prev := date1 AS pre
  FROM tableX AS t
    CROSS JOIN
      ( SELECT @d := DATE('1000-01-01')
             , @prev := @d 
      ) AS dummy
  ORDER BY date2 ASC 
) AS tmp
ORDER BY date2 DESC
;