Solved

SQL Case statement problem

Posted on 2011-09-28
3
233 Views
Last Modified: 2012-05-12
I have this :

UPDATE Nop_ProductVariant
SET Price = OldPrice + 2.00 + (
CASE
WHEN OldPrice              < 5 THEN 5.00 - (OldPrice - ProductCost)
WHEN OldPrice BETWEEN 5 AND 10 THEN 8.00 - (OldPrice - ProductCost)
WHEN OldPrice BETWEEN 10 AND 50 THEN 12.00 - (OldPrice - ProductCost)
END

But need to add another condition :

CASE
WHEN OldPrice              < 5 THEN 5.00 - (OldPrice - ProductCost)

NEED THIS INSTEAD :
If (OldPrice - ProductCost) < 5.00
      THEN 5.00 - (OldPrice - ProductCost)
       ELSE OldPrice                                    


WHEN OldPrice BETWEEN 5 AND 10 THEN 8.00 - (OldPrice - ProductCost)

NEED THIS INSTEAD :
If (OldPrice - ProductCost) < 8.00
      THEN 8.00 - (OldPrice - ProductCost)
       ELSE OldPrice    
                               

WHEN OldPrice BETWEEN 10 AND 50 THEN 12.00 - (OldPrice - ProductCost)

NEED THIS INSTEAD :
If (OldPrice - ProductCost) < 12.00
      THEN 12.00 - (OldPrice - ProductCost)
       ELSE OldPrice                                    

END

Can I still use the Case statement to do this? thanks

0
Comment
Question by:MikeMCSD
  • 2
3 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36718590
You can nest Case Statements.  Not 100% sure if I followed your logic, but here is an example.
declare @myCosts table (Id int identity(1,1), myPrice money, myOldPrice money, myCalculatedPrice money)

insert into @myCosts values (6.00, 5.00, null)
insert into @myCosts values (10.00, 12.00, null)
insert into @myCosts values (45.00, 40.00, null)
insert into @myCosts values (2.00, 3.00, null)
insert into @myCosts values (24.00, 21.00, null)

update @myCosts

	set myCalculatedPrice = 	
	
	case 
		--if Old Price < 5
		when myOldPrice < 5 then 
			
			case 
				--the variance of "my old price" and "price" < 5 so subtract the variance from 5
				when (myOldPrice - myPrice) < 5 then (5 - (myOldPrice - myPrice)) 
				
				--the variance of "my old price" and "price" >- 5 so return myOldPrice
				else myOldPrice 
			end
		
		when myOldPrice < 10 then 
		
			case 
				--the variance of "my old price" and "price" < 5 so subtract the variance from 5
				when (myOldPrice - myPrice) < 8 then (8 - (myOldPrice - myPrice)) 
				
				--the variance of "my old price" and "price" >- 5 so return myOldPrice
				else myOldPrice 
			end
		
		
		when myOldPrice < 50 then 
		
		case 
				--the variance of "my old price" and "price" < 5 so subtract the variance from 5
				when (myOldPrice - myPrice) < 12 then (12 - (myOldPrice - myPrice)) 
				
				--the variance of "my old price" and "price" >- 5 so return myOldPrice
				else myOldPrice 
			end
		
	end																 

from @myCosts

select *
from	@myCosts

Open in new window

0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36718745
thanks gohord, ..  good idea about nesting.
I think this will work, I'll test it :

SET Price = OldPrice + 2.00 + (
CASE

WHEN OldPrice              < 5 THEN
      CASE
            WHEN (OldPrice - ProductCost) < 5.00  THEN 5.00 - (OldPrice - ProductCost)             
            ELSE OldPrice
      END

WHEN OldPrice BETWEEN 5 AND 10 THEN
      CASE
            WHEN (OldPrice - ProductCost) < 8.00  THEN 8.00 - (OldPrice - ProductCost)             
            ELSE OldPrice
      END

WHEN OldPrice BETWEEN 10 AND 50 THEN
      CASE
            WHEN (OldPrice - ProductCost) < 12.00  THEN 12.00 - (OldPrice - ProductCost)             
            ELSE OldPrice
      END

END
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 36718808
Just a heads up, a case statement will automatically exit when it reaches a condition that is TRUE.

Because of this, the BETWEEN syntax isn't necessary.   You'll notice in my sample that I didn't use BETWEEN.

I guess it just boils down to what is easier for you (or the next programmer) to understand.  I honestly couldn't tell you if there was a best practice on which way to go.



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now