I am trying my first nested case statements and have put together the following from more than one example. RxStatus can have 2 values, 'ACTIVE' has 5 conditions and 'SUSPENDED' has 2. I am calculating a new future date based on another date and the future date has to be at least 14 days into the future. My query works, but doesn't quite look right.
Is there a better way?
Thank you in advance.
You don't know what you don't know...
Is there a better way?
SQL:
SELECT
,CASE WHEN RxStatus = 'ACTIVE' THEN
CASE WHEN Ratio >= 1.67 THEN GETDATE()
ELSE CASE WHEN Ratio >=0.83 AND Ratio < 1.67 THEN DATEADD(day, 14, GETDATE())
ELSE CASE WHEN Ratio < 0.83 AND Ratio > 0 THEN DATEADD(day, [DaysSupply], FillDateTime)
ELSE CASE WHEN Ratio <= 0 AND FillDateTime < DATEADD(day, 14, GETDATE()) THEN DATEADD(day, 14, GETDATE())
ELSE FillDateTime
END
END
END
END
ELSE
CASE WHEN RxStatus = 'SUSPENDED' AND FillDateTime < DATEADD(day, 14, GETDATE()) THEN DATEADD(day, 14, GETDATE())
ELSE FillDateTime
END
END AS [New Fill Date]
Thank you in advance.
You don't know what you don't know...