Skip to content
Advertisement

Calculate percentage of SQL Group BY

The following SQL statement:

SELECT FTR, COUNT(FTR) 
FROM football_data 
WHERE Matchday >= '2019-01-18' 
GROUP BY FTR

Returns the following result:

Result

Now I’m trying to get a percentage for each of those COUNT(FTR)’s.

So the sum of those 3 numbers is 153. Now I would like to get the percentage of them

Advertisement

Answer

You can use window functions, if I understand correctly:

SELECT FTR, COUNT(*),
       COUNT(*) * 1.0 / SUM(COUNT(*)) OVER () as ratio
FROM football_data 
WHERE Matchday >= '2019-01-18'
GROUP BY FTR;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement