mysql - Show Null if count is 0 -


i have following query:

 select e.employeename, date_format(`maintenancedate`, "%m %y")     month,count(`maintenanceid`) total       `maintenance` m join employee e       on m.employeeid =e.employeeid       group month,e.employeeid  

which generates result this

employeename    month   total big             apr-15  2 bar             dec-14  1 big             dec-14  1 

may know how can improve query show total of 0 each employee not have count in particular month? eg: bar have total 0 in april 2015

you need generate list of employees , months , bring in data want count. complete lists uses cross join. use left join rest of data:

select e.employeename, mon.month, count(m.maintenanceid) total   employee e cross join      (select distinct date_format(maintenancedate, '%m %y') month       maintenance      ) mon left join      maintenance m         on m.employeeid = e.employeeid ,          date_format(m.maintenancedate, '%m %y') = mon.month group m.month, e.employeeid; 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -