Calculate percentage for each row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smileyangeluv
    New Member
    • Oct 2007
    • 4

    Calculate percentage for each row

    Hi,

    Would like to get percentage for generated column. Any idea on how to do that??

    Following SQL statement

    [CODE=mysql]SELECT s.Selection_Des c, count(u.user_id )
    from tbl_system_sele ction s, tbl_user u, tbl_user_compan y c
    where s.selection_typ e = 'INDUSTRY'
    and u.User_Company_ ID = c.Company_ID
    and c.Company_Indus try_ID = s.Selection_ID
    and u.user_activate d = '1'
    group by s.selection_id[/CODE]


    gives the result of

    =============== =============== ========
    Code:
    Selection_desc  count(u.user_id)
    
    Marketing         44
    Sales               54
    Finance           100
    =============== =============== =========

    i would like to get percentage for each row

    for example Finance shd give 50% (100/(45+55+100))*10 0)

    =============== =============== ========
    Code:
    Selection_desc  count(u.user_id)  perc
    
    Marketing         45                       22.5
    Sales               55                       27.5
    Finance           100                      50
    =============== =============== =======
    Last edited by mwasif; Nov 9 '07, 10:24 AM. Reason: Added code tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    This is something I would do using a external API, like PHP. It would be much simpler than doing it via the MySQL query, if it is even possible.

    If this has to be done via MySQL I would suggest using a stored procedure.

    Comment

    • smileyangeluv
      New Member
      • Oct 2007
      • 4

      #3
      is there any way, for example, like nested query?

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by smileyangeluv
        is there any way, for example, like nested query?
        [code=sql]
        select mgr, COUNT(mgr),((co unt(mgr)/(SELECT COUNT(*) from emp)) * 100) from emp where mgr IS NOT NULL group by mgr
        [/code]

        The above query gives me the % for the no of employees unser a particular manager. try applying this logic to your query for finding the %

        I hope this helps!!

        Comment

        Working...