我有一个名为“类别”的表。我正在尝试将每个类别的百分比作为决赛桌。
Category TOTAL
Category_x 5
Category_y 10
Category_z 20
Category_a 30
Category_b 40
预期表
Category TOTAL Overall_Percentage
Category_x 5 4.76
Category_y 10 9.523
Category_z 20 19.047
Category_a 30 28.57
Category_b 40 38.09
我的代码:
SELECT Category, TOTAL, 100*(TOTAL/SUM(TOTAL)) AS Overall_Percentage
FROM Categories
GROUP BY 1,2
答案 0 :(得分:4)
使用窗口功能:
SELECT Category, TOTAL,
(TOTAL * 100.0 / SUM(TOTAL) OVER ()) AS Overall_Percentage
FROM Categories