2014-06-10 72 views
0

即時通訊尋求在本月報告日期(MTD)和往年MTD 我爲MTD的代碼是 WHERE (Month(DateCompleted) = Month(getdate()) AND YEAR (DateCompleted) = YEAR (getdate())) 有沒有一種方法可以讓我看一下過去幾年具體的月份,因爲這一個月的Sql GETDATE往年統計

例如上面的這個查詢給了我迄今爲止6月份的所有銷售額。 我想在動態報告中比較它們,所以我想查看2013年6月。 乾杯

+0

我一定要添加日期使用DateAdd() – RustyHamster

回答

1

就減1:

where Month(DateCompleted) = Month(getdate()) 
and Year(DateCompleted) = Year(getdate()) - 1 
1

它讓我感到簡單地遞減一年會做到這一點。

YEAR (DateCompleted) = YEAR (getdate())-1