mysql - get the percentage of a row count of all counts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragrid
    New Member
    • Jan 2009
    • 29

    mysql - get the percentage of a row count of all counts

    Hi All , I would like to in one query from the same table of say first names count all the occurrences of the various first names in a column called thecount and create another column called percentage that give the percentage of the count of an individual of the sum of all the counts of first names so all fname sara is 10%
    say select count(*) as thecount , fname, count(*)/sum(count(*)) as percentage and you get
    thecount fname percentage
    4 sara 10
    19 mike
    7 lou
    10 sam
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Code:
    SELECT fname, count(fname) as nameCount, count(fname) / (SELECT count(fname) FROM names)*100 as percent FROM names
    GROUP BY fname
    This SQL should do the trick.

    Comment

    • Hegli
      New Member
      • Aug 2023
      • 1

      #3
      Hi

      How do you then get it to write the percentage?

      Sorry - found the solution :)
      Last edited by Hegli; Aug 26 '23, 08:40 AM. Reason: Found the solution

      Comment

      Working...