Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize group by count results?

Tags:

postgresql

How can the results of a "group by" count be normalized by the count's sum?

For example, given:

User     Rating (1-5)
----------------------
1        3
1        4
1        2
3        5
4        3
3        2
2        3

The result will be:

User    Count   Percentage
---------------------------
1       3      .42         (=3/7)
2       1      .14         (=1/7)
3       2      .28         (...)
4       1      .14

So for each user the number of ratings they provided is given as the percentage of the total ratings provided by everyone.

like image 351
oshi2016 Avatar asked Oct 19 '22 14:10

oshi2016


1 Answers

SELECT DISTINCT ON (user) user, count(*) OVER (PARTITION BY user) AS cnt,
       count(*) OVER (PARTITION BY user) / count(*) OVER () AS percentage;

The count(*) OVER (PARTITION BY user) is a so-called window function. Window functions let you perform some operation over a "window" created by some "partition" which is here made over the user id. In plain and simple English: the partitioned count(*) is calculated for each distinct user value, so in effect it counts the number of rows for each user value.

like image 84
Patrick Avatar answered Nov 11 '22 15:11

Patrick