Select where month name appears

0

Good afternoon,

Currently I am developing an app where I list the month and sum of the consumption of each month in KW as shown in the print below:
I just do not know how I would do to instead of listing the number of the month it appears the name of the month referring to the number, at the moment I am using the following SQL command:

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   MONTH(c.data_criacao)
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao
    
asked by anonymous 30.07.2018 / 20:23

1 answer

1

I think for this we just need to call the native method of MySQL , MONTHNAME :

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   MONTHNAME(c.data_criacao)       AS mes
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao

If you want more work or if it's even needed:

SELECT      ROUND(SUM(c.potencia/12))/1000  AS potencia
        ,   CASE MONTH(c.data_criacao)
            WHEN 1 THEN 'Janeiro'
            WHEN 2 THEN 'Fevereiro'
            WHEN 3 THEN 'Março'
            WHEN 4 THEN 'Abril'
            WHEN 5 THEN 'Maio'
            WHEN 6 THEN 'Junho'
            WHEN 7 THEN 'Julho'
            WHEN 8 THEN 'Agosto'
            WHEN 9 THEN 'Setembro'
            WHEN 10 THEN 'Outubro'
            WHEN 11 THEN 'Novembro'
            ELSE 'Dezembro'
            END                             AS mes
FROM        consumo c 
WHERE       c.comodo_id = :id 
GROUP BY    c.data_criacao
    
30.07.2018 / 20:27