0

I has been reviewing some exmaples, but I didn't found what I need. It's a query that show the count of record for each agency grouped by month. Here is part of my table structure:

recid | agency_id | departure_date 

So I need to count "recid" group by Agency and Month (of the departure_date) and get total colum

  Agency_id  | JAN | FEB | MAR | APR | MAY | ........ | TOTAL 

      10        100  80    100   120   100    1200   

It seems very easy. but I cannont find the solution. Any help will be appreciate !!!

4 Answers 4

1

Try

SELECT agency_id, 
       SUM(CASE WHEN MONTH(departure_date) = 1 THEN 1 ELSE 0 END) Jan,
       SUM(CASE WHEN MONTH(departure_date) = 2 THEN 1 ELSE 0 END) Feb,
       SUM(CASE WHEN MONTH(departure_date) = 3 THEN 1 ELSE 0 END) Mar,
       ...
       COUNT(*) Total
  FROM table1
 WHERE departure_date BETWEEN '2013-01-01' AND '2013-12-31'
 GROUP BY 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 |

SQLFiddle

0
0

Try

 select agency_id, 
    sum(if(month(`departure_date`) = 1,1,0)) as 'JAN',
    sum(if(month(`departure_date`) = 2,1,0)) as 'FEB',
    sum(if(month(`departure_date`) = 3,1,0)) as 'MAR',
    sum(if(month(`departure_date`) = 4,1,0)) as 'APR',
    sum(if(month(`departure_date`) = 5,1,0)) as 'MAY',
    sum(if(month(`departure_date`) = 6,1,0)) as 'JUN',
    sum(if(month(`departure_date`) = 7,1,0)) as 'JULY',
    sum(if(month(`departure_date`) = 8,1,0)) as 'AUG',
    sum(if(month(`departure_date`) = 9,1,0)) as 'SEPT',
    sum(if(month(`departure_date`) = 10,1,0)) as 'OCT',
    sum(if(month(`departure_date`) = 11,1,0)) as 'NOV',
    sum(if(month(`departure_date`) = 12,1,0)) as 'DEC',
            count(agency_id) as TOTAL 
 from tablename
 where .....
 group by agency_id
0

Try this out:

SELECT 
    agency_id,
    SUM(IF(month = 1, numRecords, NULL)) AS 'January',
    SUM(IF(month = 2, numRecords, NULL)) AS 'Feburary',
    SUM(IF(month = 3, numRecords, NULL)) AS 'March',
    SUM(IF(month = 4, numRecords, NULL)) AS 'April',
    SUM(IF(month = 5, numRecords, NULL)) AS 'May',
    SUM(IF(month = 6, numRecords, NULL)) AS 'June',
    SUM(IF(month = 7, numRecords, NULL)) AS 'July',
    SUM(IF(month = 8, numRecords, NULL)) AS 'August',
    SUM(IF(month = 9, numRecords, NULL)) AS 'September',
    SUM(IF(month = 10, numRecords, NULL)) AS 'October',
    SUM(IF(month = 11, numRecords, NULL)) AS 'November',
    SUM(IF(month = 12, numRecords, NULL)) AS 'December',
    SUM(numRecords) AS total
    FROM (
        SELECT agency_id, month(departure_date) AS month, count(*) as numRecords
        FROM your_table_name
        GROUP BY agency_id, month
    ) AS SubTable1 GROUP BY agency_id  

For the months where no record exists for a particular agency_id, this query will displaye null. If you want to display it as 0, update the query and replace the NULL with a literal 0.

0
0

Take a look at this solution with ROLLUP clause, it show what you need, but gives another output -

SELECT
  agency_id, MONTH(departure_date) month, COUNT(*) count
FROM
  sales
GROUP BY
  agency_id, MONTH(departure_date) WITH ROLLUP
WHERE
  YEAR(departure_date) = 2013

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.