6

我需要帮助为视图编写案例陈述。基表有 2 列我将引用:“Stage”和“YesNo”。

如果 Stage 列是 1,YesNo 列是 1,我需要 CASE 语句在视图中将其显示为“否”。如果 Stage 列是 1,YesNo 列是 0,我需要 CASE 语句在视图中将其显示为“是”。如果 Stage 列是 1,YesNo 列是 NULL,我需要 CASE 语句在视图中将其显示为 NULL。如果 Stage 不是 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
4

3 回答 3

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
于 2012-12-12T14:56:03.050 回答
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
于 2012-12-12T14:56:00.897 回答
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 页面以CASE获取更多信息。

于 2012-12-12T14:55:50.530 回答