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

Confronta i valori dei timestamp e assegna un valore a ciascuno di essi nel caso in cui siano cambiati

Se ho compreso correttamente la tua esigenza, il tuo week_switch la riga dei risultati dipende solo dal tuo prodotto, non dal suo timestamp.

Quindi calcolalo in una sottoquery.

  SELECT product,
         (CASE 
          WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes' 
          ELSE 'no' END)  AS week_switch
    FROM operations
   GROUP BY product

Quindi UNISCI a quella sottoquery con i tuoi dettagli (violino ).

WITH switched AS (
  SELECT product,
         CASE
         WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes'
         ELSE 'no' END AS week_switch
    FROM operations
   GROUP BY product
)
SELECT
operations.time_stamp,
operations.product,
operations.plan_week,
switched.week_switch
FROM operations
LEFT JOIN switched ON operations.product = switched.product
WHERE time_stamp in ('2020-01-01', '2020-03-15')
GROUP BY 1,2;