Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2006
    Posts
    35

    Unanswered: Help with percentage calculation

    This is what I am using to calculate the percentage from two columns. The results look like percentages, but are wrong. Very frustrating. Please help!

    CAST(C270_admin.DNNCorrect.VoteVal * 1.0 / C270_admin.Count_TotalbyPolicy.CountPol * 100 AS DECIMAL(18 , 2))

    Thanks.

  2. #2
    Join Date
    Aug 2009
    Posts
    262
    what is your desired output ?

    if u have explained simple what is DNNconrre...... and totalpolicy........ are ..

    what is ur desired % ?

    give a sample 2 row insert statement of sample data ... and the required %

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I find debugging stuff like this is easier when you split out your function in to separate parts and see what's what. For instance does this help you spot the error?
    Code:
    SELECT C270_admin.DNNCorrect.VoteVal
         , C270_admin.DNNCorrect.VoteVal * 1.0
         , C270_admin.Count_TotalbyPolicy.CountPol
         , C270_admin.Count_TotalbyPolicy.CountPol * 100
         , C270_admin.Count_TotalbyPolicy.CountPol * 100.0
    George
    Home | Blog

  4. #4
    Join Date
    Aug 2009
    Posts
    262
    you are very correct sir gvee

    here CAST(C270_admin.DNNCorrect.VoteVal * 1.0 / C270_admin.Count_TotalbyPolicy.CountPol * 100 AS DECIMAL(18 , 2))


    C270_admin.DNNCorrect.VoteVal * 1.0
    /
    C270_admin.Count_TotalbyPolicy.CountPol * 100


    let us suppose
    v1) C270_admin.DNNCorrect.VoteVal =90 => 9000x1 => 9000
    ---------------------------------------------------------------------
    v2) C270_admin.Count_TotalbyPolicy.CountPol =20 x100 =>2070

    cast( v2 decimal18,2) since the need of using cast with scale is due to my assumption is that before hand the value did not have a scale
    hence
    v2) C270_admin.Count_TotalbyPolicy.CountPol =20.7 x100 =>2070

    9000/2070 = 4.378

    in any way i simplify that what the questionior have asked .....

    IT IS SIMPLE DIVISION ..

    and since when a simple division produce percentage ?


    this is why i ask for required percentage ( expected percentage)


    sqlserver is not windows calculator . it is a worksheet where you do your calculations as you are doing it on paper .
    ( not mentioning at all .. all the functions .... i am just plain simply talking about simple maths)

  5. #5
    Join Date
    Dec 2006
    Posts
    35

    I got it to work - somehow.

    Thanks for the replies. It works!!!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •