6

I need help writing a case statement for a view. The base table has 2 columns that I'll be referencing: 'Stage' and 'YesNo'.

If Stage column is 1, and the YesNo column is 1, I need the CASE statement to show it in the view as 'No.' If the Stage column is 1, and the YesNo column is 0, I need the CASE statement to show it in the view as 'Yes.' If the Stage column is 1, and the YesNo column is NULL, I need the CASE statement to show it in the view as NULL. If the Stage is anything other than 1, I need the YesNo column to show in the view as NULL.

This is my logic so far which I think is correct, but when I try to run it, I get a syntax error about the word 'AS'. Any suggestions?

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 に答える