How to have percentage of total on each row

(3 posts) (2 voices)

Tags:

No tags yet.

  1. phil1308, Blocked

    Hello,
    I'm new myDBR user (and french by the way, so sorry for my english).
    I'm trying to create a table from a request where, on each row I can figure the percentage from the total.
    Exemple :
    - 1. myDBR request :
    SELECT c1, COUNT(*) AS 'Nb', ??? AS '%' FROM T GROUP BY c1

    - 2. myDBR result I want :
    c1 Nb %
    a 15 30.00%
    b 25 50.00%
    c 10 20.00%

    How can I make it possible ?

    Thank you for any answers...

  2. myDBR Team, Key Master

    You can count the rows first:

    declare vCnt int;
    
    select count(*) into vCnt
    from T; select c1, count(*) as 'Nb', count(*)/vCnt*100 AS '%'
    from T
    group by c1;

    --
    myDBR Team

  3. phil1308, Blocked

    OK, thank you.
    I've tried to use a variable but I didn't already know the way to declare it and the way tu put a select result into it.
    Now, I know.
    Thank you again.


Reply

You must log in to post.