E ancora usa UDV.
SELECT dateOfCheckup,
duration,
minutes,
CASE WHEN @prev_month != monthOfCheckup
THEN total >= 20
WHEN @prev_total < 20
THEN 1
ELSE 0
END 99457Elig,
CASE WHEN @prev_month != monthOfCheckup
THEN total >= 40
WHEN @prev_total < 40
THEN 1
ELSE 0
END 99458Elig,
@prev_month := monthOfCheckup monthOfCheckup,
@prev_total := total total
FROM (select dateOfCheckup,
duration,
@cur_dur := ((case when duration like '% hour%' then substring_index(duration, ' hour', 1) * 60 else 0 end) +
(case when duration like '%min%' then substring_index(substring_index(duration, ' min', 1), ' ', -1) + 0 else 0 end)) as minutes,
CASE WHEN @year_month = date_format(dateOfCheckup, '%Y-%m')
THEN @cum_sum := @cum_sum + @cur_dur
ELSE @cum_sum := @cur_dur
END total,
@year_month := date_format(dateOfCheckup, '%Y-%m') monthOfCheckup
from patient, (SELECT @year_month:='', @cum_sum:=0, @cur_dur:=0) variables
ORDER BY dateOfCheckup) subquery,
(SELECT @prev_month:=0, @prev_total:=0) variable
ORDER BY dateOfCheckup
Uso 1
e 0
invece di Y
e NULL
.
Naturalmente, l'ordine delle colonne nell'elenco di output e l'ordinamento sono di nuovo fondamentali.