我在我的数据库中创建了6个月的数据,其中的列名称为productiondate。我为此生产日期栏提供了6个月的日期。现在我需要从这个单一的productiondate列检索月度数据的数据。如何在mysql中显示monthwise报表
我需要通过以下方式来显示
一月,二月,三月,........ DEC中单独列
我在我的数据库中创建了6个月的数据,其中的列名称为productiondate。我为此生产日期栏提供了6个月的日期。现在我需要从这个单一的productiondate列检索月度数据的数据。如何在mysql中显示monthwise报表
我需要通过以下方式来显示
一月,二月,三月,........ DEC中单独列
你没有提供有关当前的表结构的任何细节或现有的查询,但它听起来像你正在尝试枢轴数据从行到列。
MySQL没有透视功能,因此您需要使用具有CASE
表达式的聚合函数复制它。您的代码将与此类似:
select otherColumns,
max(case when month(productiondate)=1 then value end) Jan,
max(case when month(productiondate)=2 then value end) Feb,
max(case when month(productiondate)=3 then value end) Mar,
max(case when month(productiondate)=4 then value end) Apr,
max(case when month(productiondate)=5 then value end) May,
max(case when month(productiondate)=6 then value end) Jun,
....
from yourtable
group by otherColumns
你会然后替换otherColumns
与您要包含在最终结果的任何其他列。这些列将包含在GROUP BY
中。此外,您还需要将value
替换为您想要在每个月下显示的列的名称。
您需要使用GROUP BY
月份并生成月份明细报告,您可能还需要根据您的要求使用像SUM
,COUNT
,AVG
这样的聚合函数。
您可以利用MonthName
函数获取月份名称
mysql> SELECT MONTHNAME('1998-02-05');
-> 'February'
让你的查询可能看起来象下面这样:
SELECT MONTHNAME(productiondate) as mon, SUM(XYZ), COUNT(*)
FROM Your_Table_Name
GROUP BY mon
谢谢你,我得到了一些答案通过近.. – user1965826 2013-02-21 08:49:03
非常感谢,您的查询是非常有帮助.. – user1965826 2013-02-23 07:19:43