从MySQL获取Weekly SUM

时间:2016-02-22 08:31:20

标签: mysql sql

我希望从2015年7月开始每周获取一个mysql结果列表,显示来自我的表用户GROUPED BY Week的SUM或新用户。这可能吗?

结果是:

CW25/15: 100
CW26/15: 70
CW27/15: 180
....

怎么办?

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT CONCAT('CW',WEEK(date_col),'/',YEAR(date_col)) as week,
       COUNT(*) as count
FROM table_name
GROUP BY YEAR(date_col),WEEK(date_col)
ORDER BY date_col

答案 1 :(得分:0)

你可以这样做(你没有张贴你的桌子结构所以你必须调整它):

SELECT concat('CW',week(DateColumn),'/',year(DateColumn)) as weekDate,
       count(*) as cnt
FROM YourTable
GROUP BY concat('CW',week(DateColumn),'/',year(DateColumn))
ORDER BY year(DateColumn),week(DateColumn)