如何在 bigquery 中通过 sql table 迭代计算?

How to iterate a calculation through sql table in bigquery?

我有一个 table 在 bigquery 中看起来像这样。

groups  created_utc            score    count_comments  d_posts ups  downs
group1  2011-07-11T19:05:19Z    6988    3742             56     8530  1572
group2  2011-04-23T21:29:12Z    10455   4695             512    11756 1303

我想计算 ups 列作为 value of ups column per row/sum(ups) 的比率。我不确定如何将大查询中的除数应用于每一行。

我试过这个查询:

select (select sum(ups)
          from table group by ups,group )/(select count(ups)
          from table)*100 as ratio
from table
;

我收到错误:Scalar subquery produced more than one element。如何将除数应用于比率计算第一部分的每一行,类似于 python 中的 FOR 循环?

新的 table 应该是这样的:

groups  created_utc            score    count_comments  d_posts ups  downs   ratio
group1  2011-07-11T19:05:19Z    6988    3742             56     8530  1572   .42(8530/20286)
group2  2011-04-23T21:29:12Z    10455   4695             512    11756 1303   .58(11756/20286)  

使用SUM()作为解析函数求出整个table的总和。

SELECT *, ups * 1.0 / SUM(ups) OVER () AS ratio
FROM yourTable;