php - how to count mysql records for each month of the year grouped by one field -
i has been reviewing exmaples, didn't found need. it's query show count of record each agency grouped month. here part of table structure:
recid | agency_id | departure_date so need count "recid" group agency , month (of departure_date) , total colum
agency_id | jan | feb | mar | apr | may | ........ | total 10 100 80 100 120 100 1200 it seems easy. cannont find solution. appreciate !!!
try
select agency_id, sum(case when month(departure_date) = 1 1 else 0 end) jan, sum(case when month(departure_date) = 2 1 else 0 end) feb, sum(case when month(departure_date) = 3 1 else 0 end) mar, ... count(*) total table1 departure_date between '2013-01-01' , '2013-12-31' group agency_id output:
| agency_id | jan | feb | mar | apr | may | jun | jul | aug | sep | oct | nov | dec | total | --------------------------------------------------------------------------------------------- | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | | 2 | 2 | 2 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | | 3 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 |
Comments
Post a Comment