mysql - 如何在 MySQL 中按年和月分组

标签 mysql group-by

我想从源表中测量 ID 和 ATTRIBUTE 的计数,并将数据显示在下面的“所需报告”中。我正在使用 MySQL。

来源:

ID  |  DATE        |  ATTRIBUTE
--------------------------------
1   |  2012-01-14  |   XYZ
2   |  2012-03-14  |   
3   |  2012-03-15  |   XYZ
4   |  2012-04-24  |   ABC
5   |  2012-04-10  |   
6   |  2012-05-11  |   ABC

所需报告:

Count of Attribute

   YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
   ---------------------------------------------------------------------------
   2010 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2011 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2012 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 

Count of ID

   YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
   ---------------------------------------------------------------------------
   2010 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2011 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2012 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 

Percentage Complete ( Count of Attribute / Count of ID )

   YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
   ---------------------------------------------------------------------------
   2010 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2011 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 
   2012 |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0  |  0 

这是我到目前为止的代码。谢谢!另外请记住,我需要从数据中的日期字段中提取月份,但不确定如何。谢谢。

SELECT YEAR(document_filing_date),MONTH(document_filing_date),COUNT(aif_id)
FROM (a_aif_remaining)
GROUP BY YEAR(document_filing_date),MONTH(document_filing_date);

建议的答案不起作用!不知道为什么,这是我得到的错误:

"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' COUNT(CASE WHEN MONTH(document_filing_date) = 1 THEN aif_id END) AS Jan, CO' at line 1"

SELECT YEAR(document_filing_date,
  COUNT(CASE WHEN MONTH(document_filing_date) = 1 THEN aif_id END) AS Jan,
  COUNT(CASE WHEN MONTH(document_filing_date) = 2 THEN aif_id END) AS Feb,
  COUNT(CASE WHEN MONTH(document_filing_date) = 3 THEN aif_id END) AS Mar,
  COUNT(CASE WHEN MONTH(document_filing_date) = 4 THEN aif_id END) AS Apr,
  COUNT(CASE WHEN MONTH(document_filing_date) = 5 THEN aif_id END) AS May,
  COUNT(CASE WHEN MONTH(document_filing_date) = 6 THEN aif_id END) AS Jun,
  COUNT(CASE WHEN MONTH(document_filing_date) = 7 THEN aif_id END) AS Jul,
  COUNT(CASE WHEN MONTH(document_filing_date) = 8 THEN aif_id END) AS Aug,
  COUNT(CASE WHEN MONTH(document_filing_date) = 9 THEN aif_id END) AS Sep,
  COUNT(CASE WHEN MONTH(document_filing_date) = 10 THEN aif_id END) AS Oct,
  COUNT(CASE WHEN MONTH(document_filing_date) = 11 THEN aif_id END) AS Nov,
  COUNT(CASE WHEN MONTH(document_filing_date) = 12 THEN aif_id END) AS Dec,
FROM a_aif_remaining
GROUP BY YEAR(document_filing_date);

最佳答案

此查询将计算所有行,并且还将仅计算 Attribute 不为 null 的行,按行中的年和月分组:

SELECT
  Year(`date`),
  Month(`date`),
  Count(*) As Total_Rows,
  Count(`Attribute`) As Rows_With_Attribute
FROM your_table
GROUP BY Year(`date`), Month(`date`)

(这是因为 Count(*) 统计了所有的行,Count(Attibute) 统计了所有 Attribute 不为空的行)

如果您需要 PIVOT 中的表,您可以使用它来仅计算 Attribute 不为空的行:

SELECT
  Year(`date`),
  Count(case when month(`date`)=1 then `Attribute` end) As Jan,
  Count(case when month(`date`)=2 then `Attribute` end) As Feb,
  Count(case when month(`date`)=3 then `Attribute` end) As Mar,
  ...
FROM your_table
GROUP BY Year(`date`)

这是计算所有行数:

SELECT
  Year(`date`),
  Count(case when month(`date`)=1 then id end) As Jan,
  Count(case when month(`date`)=2 then id end) As Feb,
  Count(case when month(`date`)=3 then id end) As Mar,
  ...
FROM your_table
GROUP BY Year(`date`)

(或者,您可以像 kander 的回答中那样使用 Sum(Month(date)=1) 而不是计算 id)。当然,您可以将这两个查询组合成这样:

SELECT
  Year(`date`),
  Count(case when month(`date`)=1 then id end) As Jan_Tot,
  Count(case when month(`date`)=1 then `Attribute` end) As Jan_Attr,
  Count(case when month(`date`)=2 then id end) As Feb_Tot,
  Count(case when month(`date`)=2 then `Attribute` end) As Feb_Attr,
  Count(case when month(`date`)=3 then id end) As Mar_Tot,
  Count(case when month(`date`)=3 then `Attribute` end) As Mar_Attr,
  ...
FROM your_table
GROUP BY Year(`date`)

关于mysql - 如何在 MySQL 中按年和月分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019964/

相关文章:

c++ - tester.exe : 0xC0000005: Access violation reading location 0x00000000 中 0x00b21840 处的未处理异常

php - 2个日期之间的MySQL搜索

php - 无法使用 PHP 从 MySQL 查询打印数据

MySQL查询运行时间太长(简单LEFT JOIN)

MySQL从本地服务器更新远程服务器

python - Pandas 直方图按多个属性分组

mysql - 在 VALUE SELECT 语句中使用 GROUP BY 进行 INSERT - 错误 1111 (HY000) : Invalid use of group function

sql - Oracle group by 为每个聚合函数使用不同的条件

mysql - SQL 查询查找正确运行的引擎对

MySQL 使用 WHERE AVG() 选择最后 10 行