[英]SQL code for CUrrent Month Last Year
我正在使用dataadd函数尝试查找总和,其中某个日期字段在当前月份中但在前一年中。
sum(case when (mt04 >= DATEADD(MONTH,-12,getdate()) and (mt04 <= dateadd(month,-11,getdate())))
then 1 else 0
end) as [New Instructions Same Month Last Year],
这是我正在使用的报告,目前正在显示从现在到月末的数据。 例如,如果我在每月的8号运行它,则显示的是上一年当月8号以后的数据。 我需要上一年整个月的总计。
因为这在sum()
,所以将所有函数调用置于当前日期没有好处。 因此,只需使用month()
和year()
:
sum(case when year(mt04) = year(getdate()) - 1 and month(mt04) = month(getdate())
then 1 else 0
end) as [New Instructions Same Month Last Year]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.