Help with an Equation...?

  • I have this SQL statement that I am trying to show the correct response...

    First, here is the line that I am working on:

    [Sales to Referrals Ratio] = CAST((CAST(sum(CASE WHEN PRODUCTS_SOLD_1 <> '' THEN 1 ELSE 0 END) AS decimal(10,5)) + CAST(sum(CASE WHEN PRODUCTS_SOLD_2 <> '' THEN 1 ELSE 0 END)AS decimal(10,5)) / CAST(COUNT(*) AS decimal(10,5))) as decimal(10,5))

    Now the values that appear for each part of this equation are as follows:

    [Sales to Referrals Ratio] = (1 + 0) / 40

    When I run this....I get a response of '1', where I should be getting '.025'

    Even with the CAST, to a decimal, I have been unable to get it to show the correct number. I will then want to multiply this decimal by 100 so as to get a percentage...

    Does anyone see where I am going wrong?

    Thank you..

  • This should work:

    [Sales to Referrals Ratio] = SUM(CASE WHEN PRODUCTS_SOLD_1 '' THEN 1.0 ELSE 0.0 END) + SUM(CASE WHEN PRODUCTS_SOLD_2 '' THEN 1.0 ELSE 0.0 END) / COUNT(*)

  • Thank you for your reply...but it still giving me an answer of '1'...Mathematically when I divide 1 by 40 I should have a decimal of .025, which when translated into a percentage would be 25%...

    This is what I am attemtpting to do with my equation...but I can not see what I am missing????

  • If I did this correctly, after removing all the casts and sums your statement comes out to be

    1 + 0 / 40

    and of couse the 0 / 40 gets done first leaving you with 0 and then 1+ 0 = 1.

    Edit: What you need is (1 + 0) / 40

  • Always the small things that trip you up .

  • You know...it is those little things that will drive you to drink!

    Wait....I am all reeedy drinkkkng...

    THANKS!!!

  • I'm glad I don't have to drink a lot recently .

  • Well no, you don't HAVE to..., that is a wonderful choice we can make...  

    (ooops..., is my Irish showing?)

    I wasn't born stupid - I had to study.

  • Yup. Or just the fact that you're a guy and it's hot out there

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply