question

siera_gld avatar image
siera_gld asked

Multi Conditional Case Statement

I need to come up with a case statement and I'm not sure if I should do it as part of the table join or in my select statement. Bur when a Status is Price Reduced then I need to pull a price that was effective the day after an issue was closed. If the Status was not equal to "Price Reduced" I need to take the first price change in the next 30 days and use that value. If there was no difference, then I need to use "Null" Any help you can provide would be great - Here is what I have so far and I tried it in the select statement but I get errors when I try to get the statement to evaluate the date range between. CASE WHEN a.STATUS = 'Price reduced' THEN DATEADD(d, 1,a.CLOSE_DATE) IS BETWEEN p.PRC_EFF_DT AND p.PRC_END_DT CASE WHEN a.Status != 'Price reduced' THEN DATEADD(d, 30, a.CLOSE_DATE) IS BETWEEN p.PRC_EFF_DT AND p.PRC_END_DT ELSE NULL END AS p.PRICE,
t-sqlcase-statement
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
ThomasRushton avatar image
ThomasRushton answered
I try to keep the JOIN ON criteria limited to just those things that are needed to make the right matches between table rows, and do the filtering in the WHERE clause - it seems more logical that way, and, I think, makes for more maintainable code. I'm sure this discussion has been had elsewhere here... http://www.google.co.uk/search?q=%2Bjoin+%2Bwhere+%2Bsite%3Aask.sqlservercentral.com
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.