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 Percentage value for given format;
Name Count Percentege
Sathish 5 50%
Jana 3 30%
Bala 2 20%
Kindly share sql query?
Finding the percentageDivide the number that you want to turn into a percentage by the whole. In this example, you would divide 2 by 5. 2 divided by 5 = 0.4. You would then multiply 0.4 by 100 to get 40, or 40%.
Finding Percentages between two columns is straightforward. You can simply use the column names and the division operator “/” to divide values in one column by another. The result is a list of values that correspond to the result of the division of all the values in the two columns.
If we have to calculate percent of a number, divide the number by the whole and multiply by 100. Hence, the percentage means, a part per hundred. The word per cent means per 100. It is represented by the symbol “%”.
This is a slightly sexier version of some of the other answers - note the use of sum(100)
to avoid the 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
This query(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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With