calculate and decimal

  • Hi everyone,

    I've a calculate query as below but actually the result couldn't show the decimal numbers, it shows rounded with decimal zeros

    does I did anything wrong? I've tried with STR, CAST & Convert with no luck

    SELECT
    P,
    T,
    CD,
    count([ID]) AS [Total],
    SUM(COUNT(*)) OVER (PARTITION BY [P], [T]) AS TTotal,
    convert(varchar(50),count([ID])*100/SUM(COUNT(*)) OVER (PARTITION BY [P], [T])) + '%' AS Percentage
    FROM
    tbl
    GROUP BY
    [P],
    [T],
    [CD]

    result example

    test

     

  • On this line

    count([ID])*100

     

    Do this

    count([ID])*100.0

     

    Does that give you the decimal output?

  • yes, it works.

    appreciate your reply.

  • tanehome wrote:

    yes, it works.

    appreciate your reply.

    The question now is, do you understand why the fix worked and why you were getting the original issue?

    The other question is, do you know what the problem is and how to fix it when you get the possible "Divide by 0" error?

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 3 (of 3 total)

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