Als ik uw vereiste goed begrijp, uw week_switch
resultaatrij hangt alleen af van uw product, niet van het tijdstempel.
Dus bereken het in een subquery.
SELECT product,
(CASE
WHEN MIN(plan_week) <> MAX(plan_week) THEN 'yes'
ELSE 'no' END) AS week_switch
FROM operations
GROUP BY product
Voeg dan die subquery toe aan uw gegevens (fiddle ).
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;