如何计算按一个字段分组的一年中每个月的mysql记录

时间:2013-05-20 04:06:16

标签: php mysql

我一直在审查一些exmaples,但我找不到我需要的东西。 这是一个查询,显示按月分组的每个机构的记录数。 这是我的表格结构的一部分:

recid | agency_id | departure_date 

所以我需要按代理商和月份(departure_date)统计“recid”组并获得总colum

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

      10        100  80    100   120   100    1200   

看起来很容易。但我找不到解决办法。 任何帮助将不胜感激!!!

4 个答案:

答案 0 :(得分:1)

尝试

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

输出:

| 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

答案 1 :(得分:0)

尝试

 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

答案 2 :(得分:0)

试试这个:

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  

对于特定agency_id没有记录的月份,此查询将显示null。如果您想将其显示为0,请更新查询并将NULL替换为文字0

答案 3 :(得分:0)

使用ROLLUP子句查看此解决方案,它显示了您需要的内容,但提供了另一个输出 -

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