Calculating percentages with GROUP BY query

后端 未结 5 513
太阳男子
太阳男子 2020-12-05 06:28

I have a table with 3 columns which looks like this:

File    User     Rating (1-5)
------------------------------
00001    1        3
00002    1        4
000         


        
5条回答
  •  感动是毒
    2020-12-05 07:05

    In TSQL this should work

    SELECT
        User,
        Rating,
        Count(*), SUM(COUNT(*)) OVER (PARTITION BY User, Rating ORDER BY User, Rating) AS Total,
    Count(*)/(SUM(COUNT(*)) OVER (PARTITION BY User, Rating ORDER BY User, Rating)) AS Percentage
    FROM
        Results
    GROUP BY
        User, Rating
    ORDER BY
        User, Rating
    

提交回复
热议问题