我需要將一組日期分類爲'Cur。 YTD','Lst。年初至今「或」其他「。年初至今基於getdate()。我有一個用於測試的臨時表,它有一個名爲DATETIME類型的'calendar_date'列。我想出了這個邏輯,它似乎工作。我只是想知道,如果這種方法從性能角度來看是有意義的,或者如果別的東西可能會更好。當前年迄今爲止,去年迄今和其他
select calendar_date,
case when (MONTH(calendar_date) < MONTH(getdate()))
or (MONTH(calendar_date) = MONTH (getdate())
AND DAY(calendar_date) <= DAY(getdate())) then
case when YEAR(calendar_date) = YEAR(GETDATE()) then 'CYTD'
when YEAR(calendar_date) = YEAR(getdate()) - 1 then 'LYTD'
else 'Other'
end
else 'Other'
end as Tim_Tag_YTD
from #temp1