March 24, 2017 at 11:42 am
What am I doing wrong with this case statement? I am trying to get a calculated 'setup_cost' where the formula is SETUP_TIME divided by SETUP_QUANTITY multiplied by a LABOR_RATE. SETUP_TIME is usually zero except for one operation. This means that the calculated setup_cost will be zero for most operations. I thought I could use a case statement here to avoid dividing by Zero but I am still getting a divide by zero error. Any help is appreciated.
SELECT CASE
WHEN SETUP_TIME = 0 THEN '0'
ELSE (SETUP_TIME / SETUP_QUANTITY) * LABOR_RATE
END AS 'Setup_Cost'
March 24, 2017 at 11:56 am
seubanks00 - Friday, March 24, 2017 11:42 AMWhat am I doing wrong with this case statement? I am trying to get a calculated 'setup_cost' where the formula is SETUP_TIME divided by SETUP_QUANTITY multiplied by a LABOR_RATE. SETUP_TIME is usually zero except for one operation. This means that the calculated setup_cost will be zero for most operations. I thought I could use a case statement here to avoid dividing by Zero but I am still getting a divide by zero error. Any help is appreciated.SELECT CASE
WHEN SETUP_TIME = 0 THEN '0'
ELSE (SETUP_TIME / SETUP_QUANTITY) * LABOR_RATE
END AS 'Setup_Cost'
You are testing if SETUP_TIME is zero, not if SETUP_QUANTITY is zero.
March 24, 2017 at 12:04 pm
Correct - if the SETUP_TIME is equal to zero then I want to return zero. If the SETUP_TIME is other than zero I want to return the result of SETUP_TIME divided by SETUP_QUANTITY multiplied by LABOR_RATE. Am I making sense?
Thanks for taking the time to respond!
March 24, 2017 at 12:19 pm
seubanks00 - Friday, March 24, 2017 12:04 PMCorrect - if the SETUP_TIME is equal to zero then I want to return zero. If the SETUP_TIME is other than zero I want to return the result of SETUP_TIME divided by SETUP_QUANTITY multiplied by LABOR_RATE. Am I making sense?Thanks for taking the time to respond!
What do you think will happen if SETUP_QUANTITY is zero?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
March 24, 2017 at 12:43 pm
ChrisM@home - Friday, March 24, 2017 12:19 PMseubanks00 - Friday, March 24, 2017 12:04 PMCorrect - if the SETUP_TIME is equal to zero then I want to return zero. If the SETUP_TIME is other than zero I want to return the result of SETUP_TIME divided by SETUP_QUANTITY multiplied by LABOR_RATE. Am I making sense?Thanks for taking the time to respond!
What do you think will happen if SETUP_QUANTITY is zero?
I'm an idiot. Thank you! I was sure it was something I was doing wrong or not understanding how CASE worked rather than realizing that the SETUP_QUANTITY could be zero as well.
March 24, 2017 at 1:47 pm
seubanks00 - Friday, March 24, 2017 12:43 PMChrisM@home - Friday, March 24, 2017 12:19 PMseubanks00 - Friday, March 24, 2017 12:04 PMCorrect - if the SETUP_TIME is equal to zero then I want to return zero. If the SETUP_TIME is other than zero I want to return the result of SETUP_TIME divided by SETUP_QUANTITY multiplied by LABOR_RATE. Am I making sense?Thanks for taking the time to respond!
What do you think will happen if SETUP_QUANTITY is zero?
I'm an idiot. Thank you! I was sure it was something I was doing wrong or not understanding how CASE worked rather than realizing that the SETUP_QUANTITY could be zero as well.
You do remember that zero divided by anything is still zero, right? You only need to test if SETUP_QUANTITY is zero to avoid a divide by zero error.
March 25, 2017 at 7:06 pm
I also recommend that you stop using quoted identifiers for aliases and that you make the resulting datatype for the THEN and ELSE be the same so that you don't have the addition overhead of implicit conversions.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply