x

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,
more ▼

asked Mar 22, 2011 at 01:53 PM in Default

siera_gld gravatar image

siera_gld
1k 77 84 85

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

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
more ▼

answered Mar 22, 2011 at 02:06 PM

ThomasRushton gravatar image

ThomasRushton ♦
33.9k 18 20 44

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x986
x40

asked: Mar 22, 2011 at 01:53 PM

Seen: 1732 times

Last Updated: Mar 22, 2011 at 01:53 PM