我有一个图表,其中包含基于层级的事件总数。 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 警报”
我查看了案例和子查询,但我无法让代码适用于我的特定案例。
最佳答案
使用窗口函数:
select . . . ,
incidents / sum(incidents) over () as SNOW_ratio
from t;
从 MySQL 8.0 开始可以使用窗口函数。
关于mysql - 如何对从 count() 派生的值求和 (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57825966/