如果这不是您所需要的,它可能会帮助您取得一些进展,或者给您一些想法:
SELECT contest_id, YEAR(submission_date) AS submission_period, COUNT(*),
"Y" AS sub_type
FROM submissions
GROUP BY 1, 2
UNION ALL
SELECT contest_id, MONTH(submission_date) AS submission_period, COUNT(*),
"M" AS sub_type
FROM submissions
GROUP BY 1, 2
UNION ALL
SELECT contest_id, DAY(submission_date) AS submission_period, COUNT(*),
"D" AS sub_type
FROM submissions
GROUP BY 1, 2
ORDER BY 1, 4, 2
但是,如果您正在逐年查看月份而不是孤立地查看月份或年份中的每周,那么我强烈建议您查看创建“时间维度”,例如在数据仓库应用程序中使用的。这是一张包含每天记录的表格,看起来有点像这样:
Date Year Month Week Quarter Day MthName
2013-01-01 | 2013 | 2013-01 | 2013W01 | 2013Q1 | Tues | January
2013-01-02 | 2013 | 2013-01 | 2013W01 | 2013Q1 | Wed | January
..
2013-03-20 | 2013 | 2013-03 | 2013W12 | 2013Q1 | Wed | March
..
2013-05-01 | 2013 | 2013-05 | 2013W18 | 2013Q2 | Wed | May
通过将提交日期加入此表,您可以按适合您要求的任何列进行分组。
专业提示:不要称它们为我标记的那些标题,使用保留字会让你痛苦:-)