我有一个图表,其中包含基于层的事件总数。 tot_incidents中的值是从count()函数派生的。我想添加第三列,百分比,将tot_incidents中的值除以tot_incidents列的总数。
tier tot_incidents
1 77
2 27
3 47
4 2
当我尝试incidents/(sum(incidents)) as SNOW_prct
时
我收到了可爱的消息“错误:聚合函数调用可能没有嵌套的聚合或窗口函数
按应用程序层的SNOW警报'
我已经研究了案例和子查询,但是我无法让代码适合我的特定案例。
答案 0 :(得分:1)
使用窗口功能:
select . . . ,
incidents / sum(incidents) over () as SNOW_ratio
from t;
从MySQL 8.0开始,可以使用窗口功能。