简体   繁体   中英

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:

结果

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM