Calculation of the percentage of groups (*)

Select * from Namelist;
Name      Age
Sathish   25
Sathish   65
Sathish   55
Sathish   45
Sathish   35
Jana      55
Jana      25
Jana      10
Bala      55
Bala      26

How to get the Percentage value for this format;

Name   Count   Percentege
Sathish  5     50%
Jana     3     30%
Bala     2     20%

Direct access to sql query?

+5
source share
4 answers

This is a slightly sexier version of some of the other answers - pay attention to use sum(100)to avoid longer (and more mundane) count(*) * 100:)

select name, count(*) as count, sum(100) / total as percentage
from namelist
cross join (select count(*) as total from namelist) x
group by 1
+10
source

This request (not tested) should work:

SELECT Name,
COUNT(*) AS Count,
(COUNT(*) / _total ) * 100 AS Percentege
FROM Namelist,
(SELECT COUNT(*) AS _total
  FROM Namelist) AS myTotal
GROUP BY Name;
+2
source

replace the column name and try the following:

SELECT  iName, 
    COUNT(iName) AS `Count`, 
    concat(FORMAT(((COUNT(iName) * 100) / NewPeople.iCount),2),'%') AS `Percentage`
FROM   people, (SELECT COUNT(iName) AS iCount FROM people) NewPeople 
GROUP BY iName;

Conclusion:

Name   Count   Percentage
Sathish  5     50.00%
Jana     3     30.00%
Bala     2     20.00%
+1
source
select
name,
count(name) as `count`,
count(name)/(select count(*) from namelist)*100 as pct
from namelist
group by name
+1
source

All Articles