Avatar of Sriv
Sriv
 asked on

Nested Case statement

This case statement id failing.. What could be the issue
-1.0*SUM(CASE WHEN L1_IS="X1" AND CAST(ISCON AS INTEGER)=1 AND LTID=1 AND CTID = 2 AND (FA="PV" OR FA="TP " OR FA="FV") 
	THEN 
	CASE WHEN @Year! = 'Y1' THEN COALESCE(A1*1.0,0.0) 
	WHEN @Year! = 'Y2' THEN COALESCE(A2*2.0,0.0)
	WHEN @Year! = 'Y3' THEN COALESCE(A3*3.0,0.0) ELSE 0 END
	ELSE 0.0 END) AS Column 

Open in new window

Microsoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
Shaun Kline

Failing because it is not giving the expected result or because an error is occurring? If the latter, provide the error message.
Shaun Kline

BTW, are those quotation marks (") in line 1? SQL generally uses apostrophes (').
zephyr_hex (Megan)

Not sure if this is the problem, but I would recommend returning the same data type from the inner case statement.  In other words, change this:
WHEN @Year! = 'Y3' THEN COALESCE(A3*3.0,0.0) ELSE 0 END

Open in new window


to this:
WHEN @Year! = 'Y3' THEN COALESCE(A3*3.0,0.0) ELSE 0.0 END

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.