我需要帮助为视图编写案例陈述。基表有2列我将引用:'Stage'和'YesNo'。
如果Stage列为1,YesNo列为1,我需要CASE语句在视图中将其显示为“No”如果Stage列为1,YesNo列为0,我需要CASE语句在视图中将其显示为“是”。如果Stage列为1,并且YesNo列为NULL,则我需要CASE语句在视图中将其显示为NULL。如果舞台不是1,我需要YesNo列在视图中显示为NULL。
这是我的逻辑到目前为止我认为是正确的,但是当我尝试运行它时,我得到关于'AS'这个词的语法错误。有什么建议吗?
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
ELSE WHEN a.Stage = 1 and a.YesorNo = 0 THEN 'Yes'
END AS NewViewColumn
答案 0 :(得分:7)
删除ELSE WHEN
,如果您离开ELSE
,那么对于任何不符合剩余逻辑的项目,它将返回null
:
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
WHEN a.Stage = 1 and a.YesorNo = 0 THEN 'Yes'
END AS NewViewColumn
或使用:
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
WHEN a.Stage = 1 and a.YesorNo = 0 THEN 'Yes'
ELSE 'other'
END AS NewViewColumn
答案 1 :(得分:2)
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
WHEN a.Stage = 1 and a.YesorNo = 0 THEN 'Yes'
ELSE something else -- If you ignored this it will be NULL
END AS NewViewColumn
答案 2 :(得分:1)
您使用的是ELSE WHEN
,这应该是ELSE
或WHEN .. THEN ..
:
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
ELSE 'Yes'
END AS NewViewColumn
或者:
CASE
WHEN a.Stage = 1 and a.YesorNo = 1 THEN 'No'
WHEN a.Stage = 1 and a.YesorNo = 0 THEN 'Yes'
END AS NewViewColumn
查看msdn page on CASE
了解详情。