根据按日期、月份和年份分组尝试此解决方案:
-- Testing data
DECLARE @TestTable TABLE(ID INT, AddDate DATETIME)
INSERT INTO @TestTable
SELECT 1, '2009-03-04 14:58:01' UNION
SELECT 1, '2009-05-14 14:58:01' UNION
SELECT 1, '2009-05-14 14:58:02' UNION
SELECT 1, '2009-05-24 14:58:01' UNION
SELECT 1, '2009-05-24 14:58:02' UNION
SELECT 1, '2009-05-24 14:58:03' UNION
SELECT 1, '2009-07-04 14:58:01' UNION
SELECT 1, '2009-09-04 14:58:01' UNION
SELECT 1, '2010-01-04 14:58:01' UNION
SELECT 1, '2010-03-04 14:58:01' UNION
SELECT 1, '2010-03-04 14:58:02'
-- MySQL Answer
-- for each day
SELECT EXTRACT(YEAR_MONTH_DAY, AddDate), COUNT(ID) FROM @TestTable
GROUP BY EXTRACT(YEAR_MONTH_DAY, AddDate)
-- for each month
SELECT EXTRACT(YEAR_MONTH, AddDate), COUNT(ID) FROM @TestTable
GROUP BY EXTRACT(YEAR_MONTH, AddDate)
-- for each year
SELECT EXTRACT(YEAR, AddDate), COUNT(ID) FROM @TestTable
GROUP BY EXTRACT(YEAR, AddDate)
-- SQL Answer
-- for each day
SELECT dateadd(day, datediff(day, 0, AddDate)+0, 0), COUNT(ID)
FROM @TestTable
GROUP BY dateadd(day, datediff(day, 0, AddDate)+0, 0)
-- for each month
SELECT dateadd(month, datediff(month, 0, AddDate)+0, 0), COUNT(ID)
FROM @TestTable
GROUP BY dateadd(month, datediff(month, 0, AddDate)+0, 0)
-- for each year
SELECT dateadd(year, datediff(year, 0, AddDate)+0, 0), COUNT(ID)
FROM @TestTable
GROUP BY dateadd(year, datediff(year, 0, AddDate)+0, 0)