select * from tab group week
这样的语句是可以执行的。但放在其他数据库就报语法错误,因为select子句中没有包含week的聚合函数
直接select * from tab group week就可以了
week是你的表的字段,表示周!
以周一作为一周的开始, 使用mysql week行数模式5:
作为周划分的标准, 比如20170101是周天, week(20170101, 5) = 0,
分WEEK_IN_MONTH, 和WEEK_IN_YEAR两种情况进行分组:
WEEK_IN_YEAR:
# by sleest 2017/03/29 按每个日期所在一年中的第几周分组汇总SELECT WEEK(MY_DATE, 5)+1 AS WEEK_OF_YEAR, COUNT(1) AS COUNT,
GROUP_CONCAT(MY_DATE) AS INCLUDE_DATE
FROM (SELECT '2017-01-01' AS MY_DATE
UNION ALL
SELECT '2017-02-08'
UNION ALL
SELECT '2017-02-03'
UNION ALL
SELECT '2017-02-01'
UNION ALL
SELECT '2017-01-21') TMP
GROUP BY WEEK(MY_DATE, 5) + 1
结果:
WEEK_IN_MONTH:
# by sleest 2017/03/29 按每个日期所在每个月的第几周分组汇总SELECT WEEK(MY_DATE, 5) - WEEK(DATE_SUB(MY_DATE, INTERVAL DAYOFMONTH(MY_DATE) - 1 DAY), 5) + 1 AS WEEK_OF_MONTH,
COUNT(1) AS COUNT,
GROUP_CONCAT(MY_DATE) AS INCLUDE_DATE
FROM (SELECT '2017-01-01' AS MY_DATE
UNION ALL
SELECT '2017-02-08'
UNION ALL
SELECT '2017-02-03'
UNION ALL
SELECT '2017-02-01'
UNION ALL
SELECT '2017-01-21') TMP
GROUP BY WEEK(MY_DATE, 5) - WEEK(DATE_SUB(MY_DATE, INTERVAL DAYOFMONTH(MY_DATE) - 1 DAY), 5) + 1
结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)