基本上,我想在8月到5月之间为一组给定的日期提取数据。使用between运算符可以工作,只要我没有越过年份标记(即BETWEEN 8和12工作 - BETWEEN 8和5不工作)。有没有办法提取这些数据?这是我写的SQL查询:
SELECT count(*), MONTH(DateTime)
FROM Downloads
WHERE YEAR(DateTime) BETWEEN 2009 AND 2010 AND MONTH(DateTime) BETWEEN 8 AND 5
GROUP BY MONTH(DateTime)
ORDER BY MONTH(DateTime)"
感谢任何帮助。
谢谢,
Eric R。
答案 0 :(得分:4)
将BETWEEN与YEAR()和MONTH()一起使用会破坏在该列上使用索引的任何机会。我会用:
SELECT
COUNT(*) AS [count],
YEAR(my_date) AS [year],
MONTH(my_date) AS [month]
FROM
Downloads
WHERE
my_date >= '2009-08-01' AND
my_date < '2010-06-01'
GROUP BY
YEAR(my_date),
MONTH(my_date)
ORDER BY
YEAR(my_date), MONTH(my_date)
(我使用了my_date,因为我无法将自己引用为DateTime的列):))
答案 1 :(得分:1)
SELECT count(*), MONTH(DateTime)
FROM Downloads
WHERE DateTime>'2009/8/1 00:00:00' AND datetime<'2010/6/1 00:00:00'
GROUP BY MONTH(DateTime)
ORDER BY MONTH(DateTime)