Sql Getdate在前几年的统计数据

时间:2014-06-10 16:03:08

标签: sql getdate

我希望报告月初至今(MTD)和前几年的MTD 我的MTD代码是 WHERE (Month(DateCompleted) = Month(getdate()) AND YEAR (DateCompleted) = YEAR (getdate ())) 有没有办法让我可以看看去年这个月的确切月份

例如,上面这个查询给了我到目前为止2014年6月的所有销售额。 我想在动态报告中对它们进行比较,所以我想查看2013年6月。 干杯

2 个答案:

答案 0 :(得分:1)

令我感到震惊的是,简单地减少年份就可以做到这一点。

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

答案 1 :(得分:1)

只需减去1:

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