我想创建一个"计算"列基于" if ... else"逻辑。 我试过了:
x.ApplicationID
,CASE WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 7 THEN 1
WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 14 THEN 2
WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 30 THEN 3
ELSE 0
END AS Prodleni
引发错误:
关键字&#39; WHEN&#39;附近的语法不正确。
帮助将不胜感激。
答案 0 :(得分:3)
只需要其他1个。你需要使用如下 -
x.ApplicationID
,CASE WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 7 THEN 1
WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 14 THEN 2
WHEN DATEDIFF(dd, x.CreateDate, GETDATE()) < 30 THEN 3
ELSE 0
END AS Prodleni
了解更多信息 https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql