Skip to main content
added 2 characters in body
Source Link
Aaron Bertrand
  • 181.5k
  • 28
  • 402
  • 619

You need to either repeat the expression, or derive it in a separate scope, e.g.:

SELECT name, tag, tag2 = CASE
   WHEN tag = 'a' THEN 'a2' 
   ELSE 'b2' END AS tag2
FROM
(
   SELECT  name, tag = CASE  
        WHEN name = 'a' THEN 'a'  
        ELSE 'b'b' END AS tag
   FROM dbo.table_name
) AS derived;

You need to either repeat the expression, or derive it in a separate scope, e.g.:

SELECT name, tag, tag2 = CASE
   WHEN tag = 'a' THEN 'a2' 
   ELSE 'b2' END
FROM
(
   SELECT  name, tag = CASE  
        WHEN name = 'a' THEN 'a'  
        ELSE 'b END 
   FROM dbo.table_name
) AS derived;

You need to either repeat the expression, or derive it in a separate scope, e.g.:

SELECT name, tag, CASE
   WHEN tag = 'a' THEN 'a2' 
   ELSE 'b2' END AS tag2
FROM
(
   SELECT  name, CASE  
        WHEN name = 'a' THEN 'a'  
        ELSE 'b' END AS tag
   FROM dbo.table_name
) AS derived;
Source Link
Aaron Bertrand
  • 181.5k
  • 28
  • 402
  • 619

You need to either repeat the expression, or derive it in a separate scope, e.g.:

SELECT name, tag, tag2 = CASE
   WHEN tag = 'a' THEN 'a2' 
   ELSE 'b2' END
FROM
(
   SELECT  name, tag = CASE  
        WHEN name = 'a' THEN 'a'  
        ELSE 'b END 
   FROM dbo.table_name
) AS derived;