Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como obter partição por tipo de funcionalidade usando variáveis ​​no MySQL 5.7


E novamente use 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

violino

Eu uso 1 e 0 em vez de Y e NULL .

Obviamente, a ordem das colunas na lista de saída e a ordenação são críticas novamente.