SQL查询如何查找一月(混年)最后几个月的数据SQLServer 2008
SQL Query How to Find Data for Last Few Months in January (mixed years) SQL Server 2008
我正在创建查询以提取过去 3 个月的记录。我遇到的问题是最后 x 个月跨到前一年。我想我有一个解决方案,但我觉得有更好的方法吗?
字段:asiplayer.date_used 是日期时间字段。
下面的代码似乎有效(但我如何在 7 月测试 1 月?)
有没有更好的方法?如果是最近 6 个月怎么办,我会继续这个代码吗?
WHERE
-- ACTIVE LIST (LAST 3 MONTHS)
(
( --Current Month is Not January or February, so all 3 months are in current year
MONTH(CURRENT_TIMESTAMP) NOT IN (1,2)
AND MONTH(asiplayer.date_used) >= MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP))
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
OR --It's January so some of the months are last year
(
--Get November and December of the Previous Year
(MONTH(CURRENT_TIMESTAMP) = 1 AND (MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP)) OR MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-1,CURRENT_TIMESTAMP)))
AND YEAR(asiplayer.date_used) = YEAR(DateAdd(year,-1,CURRENT_TIMESTAMP))
)
OR
--Get January of the Current Year
(MONTH(CURRENT_TIMESTAMP) = 1
AND MONTH(asiplayer.date_used) = 1
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
)
OR --It's February so some of the months are last year
(
--Get December of the Previous Year
(MONTH(CURRENT_TIMESTAMP) = 2 AND MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP))
AND YEAR(asiplayer.date_used) = YEAR(DateAdd(year,-1,CURRENT_TIMESTAMP))
)
OR
--Get January & February of the Current Year
(MONTH(CURRENT_TIMESTAMP) = 2
AND MONTH(asiplayer.date_used) IN (1,2)
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
)
)
Serg 提供了答案:
DATEDIFF(MONTH, asiplayer.date_used, CURRENT_TIMESTAMP) <=3
我正在创建查询以提取过去 3 个月的记录。我遇到的问题是最后 x 个月跨到前一年。我想我有一个解决方案,但我觉得有更好的方法吗?
字段:asiplayer.date_used 是日期时间字段。
下面的代码似乎有效(但我如何在 7 月测试 1 月?)
有没有更好的方法?如果是最近 6 个月怎么办,我会继续这个代码吗?
WHERE
-- ACTIVE LIST (LAST 3 MONTHS)
(
( --Current Month is Not January or February, so all 3 months are in current year
MONTH(CURRENT_TIMESTAMP) NOT IN (1,2)
AND MONTH(asiplayer.date_used) >= MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP))
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
OR --It's January so some of the months are last year
(
--Get November and December of the Previous Year
(MONTH(CURRENT_TIMESTAMP) = 1 AND (MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP)) OR MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-1,CURRENT_TIMESTAMP)))
AND YEAR(asiplayer.date_used) = YEAR(DateAdd(year,-1,CURRENT_TIMESTAMP))
)
OR
--Get January of the Current Year
(MONTH(CURRENT_TIMESTAMP) = 1
AND MONTH(asiplayer.date_used) = 1
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
)
OR --It's February so some of the months are last year
(
--Get December of the Previous Year
(MONTH(CURRENT_TIMESTAMP) = 2 AND MONTH(asiplayer.date_used) = MONTH(DateAdd(month,-2,CURRENT_TIMESTAMP))
AND YEAR(asiplayer.date_used) = YEAR(DateAdd(year,-1,CURRENT_TIMESTAMP))
)
OR
--Get January & February of the Current Year
(MONTH(CURRENT_TIMESTAMP) = 2
AND MONTH(asiplayer.date_used) IN (1,2)
AND YEAR(asiplayer.date_used) = YEAR(CURRENT_TIMESTAMP)
)
)
)
Serg 提供了答案:
DATEDIFF(MONTH, asiplayer.date_used, CURRENT_TIMESTAMP) <=3