?
Solved

case statement not following desire logic correctly

Posted on 2014-03-11
3
Medium Priority
?
256 Views
Last Modified: 2014-03-11
I am trying to input the pseudo-code below into a sql case statement

but i think is not working because the 3rd scenario is not applying when appropriate.

pseudo code:
A. If “Requested Price” have value then calculate “Requested Price” minus “Total Cost” divided “Requested Price” equal “Expected Margin”
B. If “Suggested Price” have value then calculate “Suggested Price” minus “Total Cost” divided “Suggested Price” equal “Expected Margin”
C. If “Requested Price” and “Suggested Price” both have values then calculate “Suggested Price” minus “Total Cost” divided “Suggested Price” equal “Expected Margin”


,	ExpectedMargin = CASE 
						WHEN [sql].[SalesQuotePriceRequested] <> 0 
						THEN (
								[sql].[SalesQuotePriceRequested]			
								- (										
									ROUND([sql].UnitCostMaterial,2)
									+ROUND([sql].[TotalLabor_OHCost], 2)
									+ROUND([sql].[UnitCostFreight], 2)
									+[sql].[UnitCostSGA]	
									+ROUND([sql].[UnitCostDiscount], 2)
									+[sql].[AdditionalCost]
								  )										
							 ) / [sql].[SalesQuotePriceRequested]
						WHEN [sql].[SuggestedPrice] <> 0
						THEN (
								[sql].[SuggestedPrice]					
								- (										
									ROUND([sql].UnitCostMaterial,2)
									+ROUND([sql].[TotalLabor_OHCost], 2)
									+ROUND([sql].[UnitCostFreight], 2)
									+[sql].[UnitCostSGA]	
									+ROUND([sql].[UnitCostDiscount], 2)
									+[sql].[AdditionalCost]
								  )										
							 ) / [sql].[SuggestedPrice] 
						WHEN ([sql].[SalesQuotePriceRequested] <> 0 AND [sql].[SuggestedPrice] <> 0)
						THEN (
								[sql].[SuggestedPrice]					
								- (										
									ROUND([sql].UnitCostMaterial,2)
									+ROUND([sql].[TotalLabor_OHCost], 2)
									+ROUND([sql].[UnitCostFreight], 2)
									+[sql].[UnitCostSGA]	
									+ROUND([sql].[UnitCostDiscount], 2)
									+[sql].[AdditionalCost]
								  )										
							 ) / [sql].[SuggestedPrice] 
				     END

Open in new window

0
Comment
Question by:metropia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1100 total points
ID: 39921818
For starters, wayy too many {TAB} characters.

After that, I recommend a nesting that goes something like this, with your third block moved to the first, as in the current order both 1 and 2 will be correct before 3, which is causing unnecessary processing.

<air code>
CASE
  WHEN Requested Price IS NOT NULL AND  “Suggested Price” IS NOT NULL
      THEN Expected Margin = --  that calculation goes here
  WHEN Requested Price IS NOT NULL 
      THEN Expected Margin = --  that calculation goes here
  WHEN Suggested Price IS NOT NULL 
      THEN Expected Margin = --  that calculation goes here
END

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 900 total points
ID: 39921819
You have to do the last WHEN first, otherwise it's never reached:


                                    WHEN ([sql].[SalesQuotePriceRequested] <> 0 AND [sql].[SuggestedPrice] <> 0)
                                    THEN (
                                                [sql].[SuggestedPrice]                              
                                                - (                                                            
                                                      ROUND([sql].UnitCostMaterial,2)
                                                      +ROUND([sql].[TotalLabor_OHCost], 2)
                                                      +ROUND([sql].[UnitCostFreight], 2)
                                                      +[sql].[UnitCostSGA]      
                                                      +ROUND([sql].[UnitCostDiscount], 2)
                                                      +[sql].[AdditionalCost]
                                                  )
0
 

Author Closing Comment

by:metropia
ID: 39921976
thank you experts!
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

801 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