即使几个月内没有数据,我也会使用LEFT JOIN表日历来显示几个列。如果我说tblCalendar.Year = 2016
它就会有效,所以它会给我带来当前一年的12个月。
但是我需要从本月末开始一年的数据。
所以我使用的是数据范围
where cal.YearNum >= YEAR(GETDATE())-1 AND cal.YearNum <=YEAR(GETDATE())
AND cal.MonthNum >=MONTH(EOMONTH(GETDATE()) )+1 AND cal.MonthNum <=MONTH(GETDATE())
由于某种原因它不会给我任何东西。
我的查询是这样的:
with cte_Data
AS (
select Month(effectiveDate) as MonthNum
Year(EffectiveDate) as YearNum
from MyTable
where EffectiveDate >=DateAdd(yy, -1, DATEADD(d, 1, EOMONTH(GETDATE()))) AND EffectiveDate <= EOMONTH(GETDATE())
)
select *
from tblCalendar cal left join cte_Data cd ON cal.MonthNum=cd.ClrEffMonth AND cal.YearNum=cd.ClrEffYear
where cal.YearNum >= YEAR(GETDATE())-1 AND cal.YearNum <=YEAR(GETDATE())
AND cal.MonthNum >=MONTH(EOMONTH(GETDATE()) )+1 AND cal.MonthNum <=MONTH(GETDATE())
答案 0 :(得分:0)
WHERE ( b.YearNum = YEAR(GETDATE())-1 and b.MonthNum >= MONTH(GETDATE())+1 ) OR
( b.YearNum = YEAR(GETDATE()) and b.MonthNum <= MONTH(GETDATE()) )