I have the following SQL query (VERY simplified):
SELECT
Column1,
Column2,
Column3,
Column4,
CASE
WHEN [Field1] = Condition1 THEN Value1
WHEN [Field1] = Condition2 THEN Value2
ELSE Value2
END AS "Label1",
CASE @Var
WHEN Condition 1 THEN
CASE [Field2]
WHEN Condition1 THEN Value1
WHEN Condition2 THEN Value2
WHEN Condition3 THEN Value3
WHEN Condition4 THEN Value4
ELSE Value2
END
ELSE
CASE [Field2]
WHEN Condition5 THEN Value5
WHEN Condition6 THEN Value6
WHEN Condition7 THEN Value7
WHEN Condition8 THEN Value8
ELSE Value2
END
END AS "Label2",
I need to use the result of the first CASE expression to evaluate the second CASE expression. I thought that using a variable (@Var) would be the best way, however I can't get the result of the first CASE expression assigned to the variable.
I have tried:
SET @Varin front of theCASEclauseSET @Var =in eachWHENclause- Removing the
AS Label1from the end of theCASEexpression - Making two copies of the
CASEexpression, using one of the variable and the other for theField1value
Every attempt to add the variable to the first CASE expression returns an error. I've been able to add the nested CASE expression (and test it) properly, but I need to be able to examine the result of the first CASE expression in order to properly set the second.
I'm not fixed on using a variable to accomplish this requirement.