How to Calculate Percentage of Counts in SQL

SQL - Calculate percentage on count(column)

SELECT event, 
count(event) as event_count,
count(event) * 100.0 / (select count(*) from event_information) as event_percent
FROM event_information
group by event

How to calculate percentage with a SQL statement

I have tested the following and this does work. The answer by gordyii was close but had the multiplication of 100 in the wrong place and had some missing parenthesis.

Select Grade, (Count(Grade)* 100 / (Select Count(*) From MyTable)) as Score
From MyTable
Group By Grade

Calculate percentages of count in SQL relative to the category

I would use window functions to compute the average, then aggregation:

select waiting, is_cancelled, 
count(*) / sum(count(*)) over(partition by waiting) as ratio
from (
select b.*,
case when days_in_waiting_list > avg(days_in_waiting_list) over()
then 'Long Wait'
else 'Short wait'
end as waiting
from bookings b
) b
group by waiting, is_cancelled
order by waiting, is_cancelled

Calculate Percentage from Count of Two Tables

select (
Select (COUNT(DISTINCT dat.CoAc) * 100) AS Count1 /*count1 * 100*/
From(
Select *
From IR20181125
where pdate not in ('NULL','')
) dat
) / (
Select COUNT(DISTINCT CoAc) As Count2
From IPR20181125
) CountPercentage

Let me know, what did you got.

Update 1

select (
Select convert (decimal (18,2), (COUNT(DISTINCT dat.CoAc) * 100)) AS Count1
From(
Select *
From IR20181125
where pdate not in ('NULL','')
) dat
) / (
Select convert (decimal (18,2), COUNT(DISTINCT CoAc)) As Count2
From IPR20181125
) CountPercentage

How can I calculate the percentage, for each row, of the sum of the COUNT(*) in a grouped query? - SQL

Below the query will help you to meet the requirement

select sport,count(*) as count,(count(*)*100)/(Select count(*) from goldmedal where 
country = "Norway") as percent
from goldmedal where country = "Norway" group by sport;

Calculating % of COUNT with groupby function in bigquery

How to calculate the percentage of employee type in OTBI Report?

You may try using analytic functions here:

SELECT Name, "Employee Type",
100.0 * (COUNT(*) OVER (PARTITION BY "Employee Type") /
COUNT(*) OVER () ) AS Percentage
FROM yourTable
ORDER BY "Employee Type", Name;

Here is a demo for Oracle.

If for some reason you can't use analytic functions, then a join approach also works here:

SELECT t1.Name, t1."Employee Type",
100.0 * t2.cnt / (SELECT COUNT(*) FROM yourTable) AS Percentage
FROM yourTable t1
INNER JOIN
(
SELECT "Employee Type", COUNT(*) AS cnt
FROM yourTable
GROUP BY "Employee Type"
) t2
ON t2."Employee Type" = t1."Employee Type"
ORDER BY "Employee Type", Name;


Related Topics



Leave a reply



Submit