[英]how to count mysql records for each month of the year grouped by one field
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 因此,我需要按代理商和月份(“ department_date”的月份)来计算“收据”组,并获得总
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 !!!
任何帮助将不胜感激!
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 |
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
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
. 对于特定的agency_id没有记录的月份,此查询将显示
null
。 If you want to display it as 0
, update the query and replace the NULL
with a literal 0
. 如果要将其显示为
0
,请更新查询并将NULL
替换为文字0
。
Take a look at this solution with ROLLUP clause, it show what you need, but gives another output - 看看带有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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.