sql - MySQL select by month returns NULL for all months -


using mysql 5.6 trying select total inspections each month in past 12 months output of 0 if there none. appear missing here output null. date_inspected field regular sql date

from understood, structure should [conditional statement, output variable, default value] 0 gets ignored in favor of null. trying understand doing wrong here.

table:

column          type        null     id              int(11)     no            inspector_id    int(11)     yes           company_id      int(11)     yes       date_inspected  date        no            start_time      datetime    no            end_time        datetime    no        

query:

select   sum(if(month = 'jan', total, 0)) 'januari',   sum(if(month = 'feb', total, 0)) 'februari',   sum(if(month = 'mar', total, 0)) 'maart',   sum(if(month = 'apr', total, 0)) 'april',   sum(if(month = 'may', total, 0)) 'mei',   sum(if(month = 'jun', total, 0)) 'juni',   sum(if(month = 'jul', total, 0)) 'juli',   sum(if(month = 'aug', total, 0)) 'augustus',   sum(if(month = 'sep', total, 0)) 'september',   sum(if(month = 'oct', total, 0)) 'oktober',   sum(if(month = 'nov', total, 0)) 'november',   sum(if(month = 'dec', total, 0)) 'december',   sum(total) all_months   (         select month(date_inspected) month, count(*) total         inspection         date_inspected between now() , date_add(now(), interval - 12 month)         group month   ) subtable 

output

{ ["januari"]=> null  ["februari"]=> null  ["maart"]=> null  ["april"]=> null  ["mei"]=> null  ["juni"]=> null  ["juli"]=> null  ["augustus"]=> null  ["september"]=> null  ["oktober"]=> null  ["november"]=> null  ["december"]=> null  ["all_months"]=> null } 

update:

solution gordon linoff

select      sum(case when month(date_inspected) = 1 1 else 0 end) 'januari',      sum(case when month(date_inspected) = 2 1 else 0 end) 'februari',      sum(case when month(date_inspected) = 3 1 else 0 end) 'maart',      sum(case when month(date_inspected) = 4 1 else 0 end) 'april',      sum(case when month(date_inspected) = 5 1 else 0 end) 'mei',      sum(case when month(date_inspected) = 6 1 else 0 end) 'juni',      sum(case when month(date_inspected) = 7 1 else 0 end) 'juli',      sum(case when month(date_inspected) = 8 1 else 0 end) 'augustus',      sum(case when month(date_inspected) = 9 1 else 0 end) 'september',      sum(case when month(date_inspected) = 10 1 else 0 end) 'oktober',      sum(case when month(date_inspected) = 11 1 else 0 end) 'november',      sum(case when month(date_inspected) = 12 1 else 0 end) 'december' inspection date_inspected between date_add(now(), interval - 12 month) ,  now() 

as understand it, we've given sum() conditional case statement if current record's date_inspected's month equal mysql constant value, return true , add total, else nothing.

more on mysql case

juergen correct on 1 problem in query. month() returns number, not string.

and, can further simplify query. subquery not needed:

    select sum(case when month(date_inspected) = 1 1 else 0 end)) 'januari',            sum(case when month(date_inspected) = 2 1 else 0 end)) 'februari',            . . .     inspection     date_inspected between date_add(now(), interval - 12 month) , now(); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -