Boolean logic with nested case Example in SQL

8 Jul

SELECT ERI_Organization_KEY, ROUND(ROUND(R_2010P06,2),1),ROUND(ROUND(RQ_2010P06,2),1),ROUND(ROUND(RY_2010P06,2),1),
CASE WHEN R_2010P06 >= ‘0’ THEN
CASE WHEN ROUND(ROUND(R_2010P06,2),1) <= ‘0.14’ THEN ‘3.0A’
WHEN ROUND(ROUND(R_2010P06,2),1) <= ‘0.55’ THEN ‘4.0A’
WHEN ROUND(ROUND(R_2010P06,2),1) >= ‘0.56’ THEN ‘5.0A’
END
ELSE
CASE WHEN R_2010P06 < ‘0’ THEN
CASE WHEN ROUND(ROUND(R_2010P06,2),1) <= ‘-0.54’ THEN ‘1.0B’
WHEN ROUND(ROUND(R_2010P06,2),1) <= ‘-0.14’ THEN ‘2.0B’
WHEN ROUND(ROUND(R_2010P06,2),1) <= ‘-0.01’ THEN ‘3.0B’ END
END END AS R_2010P06,
CASE WHEN RQ_2010P06 >= ‘0’ THEN
CASE WHEN ROUND(ROUND(RQ_2010P06,2),1) <= ‘0.14’ THEN ‘3.0A’
WHEN ROUND(ROUND(RQ_2010P06,2),1) <= ‘0.55’ THEN ‘4.0A’
WHEN ROUND(ROUND(RQ_2010P06,2),1) >= ‘0.56’ THEN ‘5.0A’
END
ELSE
CASE WHEN RQ_2010P06 < ‘0’ THEN
CASE WHEN ROUND(ROUND(RQ_2010P06,2),1) <= ‘-0.54’ THEN ‘1.0B’
WHEN ROUND(ROUND(RQ_2010P06,2),1) <= ‘-0.14’ THEN ‘2.0B’
WHEN ROUND(ROUND(RQ_2010P06,2),1) <= ‘-0.01’ THEN ‘3.0B’ END
END END AS RQ_2010P06,
CASE WHEN RY_2010P06 >= ‘0’ THEN
CASE WHEN ROUND(ROUND(RY_2010P06,2),1) <= ‘0.14’ THEN ‘3.0A’
WHEN ROUND(ROUND(RY_2010P06,2),1) <= ‘0.55’ THEN ‘4.0A’
WHEN ROUND(ROUND(RY_2010P06,2),1) >= ‘0.56’ THEN ‘5.0A’
END
ELSE
CASE WHEN RY_2010P06 < ‘0’ THEN
CASE WHEN ROUND(ROUND(RY_2010P06,2),1) <= ‘-0.54’ THEN ‘1.0B’
WHEN ROUND(ROUND(RY_2010P06,2),1) <= ‘-0.14’ THEN ‘2.0B’
WHEN ROUND(ROUND(RY_2010P06,2),1) <= ‘-0.01’ THEN ‘3.0B’ END
END END AS RY_2010P06
FROM BSC_R_COS_2010P06

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: