Solved

case statement not following desire logic correctly

Posted on 2014-03-11
3
249 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
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 275 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 225 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 28
sql server computed columns 11 31
Unable to Uninstall Visual Studio 2015 7 26
How to use three values with DATEDIFF 3 18
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

776 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