MySQL = 按月份对销售计数进行分组,然后将月份显示为名称(一月、二月...十二月)而不是数值 (1,2,3-12)
MySQL = Grouping Sales Count by Months, then Displaying months as Names (Jan, Feb...DEC) not Numerical Value (1,2,3-12)
这似乎是一个相当简单的问题,但我不知道如何在销售计数按月份分组后将月份的数值更改为月份的实际名称。
基本上我想列出每个月的销售总额。目前这是我拥有的:
SELECT MONTH(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate);
这对我想做的事情非常有用。并给出以下结果:
+---------------------------------+
| Month | TotalSales |
+---------------------------------+
| 1 | 123 |
| 2 | 142 |
| 3 | 183 |
| 4 | 99 |
| 5 | 127 |
| 6 | 202 |
+---------------------------------+
etc....
但是我希望列表中的月份为一月、二月...等等。
我尝试了多种使用 MONTHNAME()、MONTHNAME(STR_TO_DATE) 和许多其他方法的方法,但将月份分组似乎会导致问题。
我觉得我缺少一个非常简单的解决方案,但无法在网上找到任何解决方案。作为参考,销售 table 中的日期是默认日期(2017-01-01 或 YYYY-MM-DD)。而saletable有3个属性:SaleID、StoreID和SaleDate。
+---------------------------------+
| Month | TotalSales |
+---------------------------------+
| January | 123 |
| February | 142 |
| March | 183 |
| April | 99 |
| May | 127 |
| June | 202 |
+---------------------------------+
and so on...
这是我从我尝试过的方法中收到的错误消息:
Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY
clause and contains nonaggregated column 'UsedCarDealer.Sale.SaleDate'
which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
谢谢!
试试这个:
SELECT MONTHNAME(STR_TO_DATE(MONTH(SaleDate), '%m')) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate);
我建议:
SELECT MONTHNAME(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate), MONTHNAME(SaleDate)
ORDER BY MONTH(SaleDate);
这也会 return 按时间顺序排列的值。注意:使用月份时,将年份作为过滤器或包含在 GROUP BY
中通常是有意义的。也许更好的查询版本是:
SELECT YEAR(SaleDate) as Year, MONTHNAME(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY YEAR(SaleDate), MONTH(SaleDate)
ORDER BY MIN(SaleDate);
这似乎是一个相当简单的问题,但我不知道如何在销售计数按月份分组后将月份的数值更改为月份的实际名称。
基本上我想列出每个月的销售总额。目前这是我拥有的:
SELECT MONTH(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate);
这对我想做的事情非常有用。并给出以下结果:
+---------------------------------+
| Month | TotalSales |
+---------------------------------+
| 1 | 123 |
| 2 | 142 |
| 3 | 183 |
| 4 | 99 |
| 5 | 127 |
| 6 | 202 |
+---------------------------------+
etc....
但是我希望列表中的月份为一月、二月...等等。
我尝试了多种使用 MONTHNAME()、MONTHNAME(STR_TO_DATE) 和许多其他方法的方法,但将月份分组似乎会导致问题。
我觉得我缺少一个非常简单的解决方案,但无法在网上找到任何解决方案。作为参考,销售 table 中的日期是默认日期(2017-01-01 或 YYYY-MM-DD)。而saletable有3个属性:SaleID、StoreID和SaleDate。
+---------------------------------+
| Month | TotalSales |
+---------------------------------+
| January | 123 |
| February | 142 |
| March | 183 |
| April | 99 |
| May | 127 |
| June | 202 |
+---------------------------------+
and so on...
这是我从我尝试过的方法中收到的错误消息:
Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'UsedCarDealer.Sale.SaleDate' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
谢谢!
试试这个:
SELECT MONTHNAME(STR_TO_DATE(MONTH(SaleDate), '%m')) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate);
我建议:
SELECT MONTHNAME(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY MONTH(SaleDate), MONTHNAME(SaleDate)
ORDER BY MONTH(SaleDate);
这也会 return 按时间顺序排列的值。注意:使用月份时,将年份作为过滤器或包含在 GROUP BY
中通常是有意义的。也许更好的查询版本是:
SELECT YEAR(SaleDate) as Year, MONTHNAME(SaleDate) as "Month", Count(*) as "TotalSales"
FROM Sale
GROUP BY YEAR(SaleDate), MONTH(SaleDate)
ORDER BY MIN(SaleDate);