如何计算月份行数

时间:2013-04-15 05:54:54

标签: mysql date count rows

通过此查询,我可以计算

Created By  ProductGroup           Count
HNSHAK      MOBILE COMPUTING        13
HNSHAK      STORAGE                 2

查询:

SELECT `Created By`, ProductGroup, COUNT(*) `Count`
FROM tablename
GROUP BY `Created By`, ProductGroup;

现在我想像MARCH / APRIL或04-2013 / 03-2013一样聪明地计算,如果有可能我有一个数据库,其中超过15000个条目我想显示每个用户计数月份...

我想要这样的输出

Created By  ProductGroup           Count    MONTH
HNSHAK      MOBILE COMPUTING        1       MARCH
HNSHAK      STORAGE                 2       MARCH


Created By  ProductGroup           Count    MONTH
HNSHAK      MOBILE COMPUTING        2       APRIL
HNSHAK      STORAGE                 1       APRIL

Created By  ProductGroup           Count    MONTH
HNSHAK      MOBILE COMPUTING        1       FEBRUARY

表:

Productgroup    Created By  Initial Intl Created
STORAGE             HNSHAK  13.02.2013
SMART+MOBIL+FIX PH. KFKAIS  19.03.2013
SMART+MOBIL+FIX PH. KFKAIS  20.03.2013
SMART+MOBIL+FIX PH. KFKAIS  19.03.2013
SCANNERS            MDSALA  20.02.2013
STORAGE             HNSHAK  07.03.2013
STORAGE             HNSHAK  13.03.2013
MOBILE COMPUTING    HNSHAK  01.03.2013
MOBILE COMPUTING    HNSHAK  01.04.2013
MOBILE COMPUTING    HNSHAK  01.04.2013
PTV/FLAT            WSAHMA  12.03.2013
PTV/FLAT            WSAHMA  09.03.2013
STORAGE             HNSHAK  13.03.2013

2 个答案:

答案 0 :(得分:1)

<强> MonthName

select [created by],
       ProductGroup,
       Count(Productgroup),
       MonthName([Initial Intl Created])
from tablename
group by [created by],ProductGroup,MonthName([Initial Intl Created])

答案 1 :(得分:0)

SELECT CreatedBy, Productgroup, COUNT(*) AS Count, DATE_FORMAT(CreateDate, '%Y-%m') AS Period
FROM tablename 
GROUP BY CreatedBy, Productgroup, Period ;

您只需添加月份/期间组。

示例:http://sqlfiddle.com/#!8/3f435/1