gpt4 book ai didi

sql - 大查询 : Divided by sum of values in column to find the ratio

转载 作者:行者123 更新时间:2023-12-04 00:32:11 24 4
gpt4 key购买 nike

我有一个简单的表,有两列 Bin_name (int) 和 Count_in_this_bin (int)

我想将其转换为每个 bin 与所有 bin 中总计数的比率。

我在 Google BigQuery 中使用了以下查询:

SELECT count_in_bin/(SELECT SUM(count_in_bin) FROM [table])
FROM [table]

然后我得到

error:Query Failed Error: Subselect not allowed in SELECT clause

现在谁能告诉我在 BigQuery 中进行这种简单划分的正确方法?

最佳答案

BigQuery 旧版 SQL

#legacySQL
SELECT
count_in_bin,
RATIO_TO_REPORT(count_in_bin) OVER() AS ratio
FROM [project:dataset.table]

BigQuery 标准 SQL

#standardSQL
SELECT
count_in_bin,
count_in_bin / total AS ratio
FROM `project.dataset.table`,
(SELECT SUM(count_in_bin) total FROM `project.dataset.table`)

关于sql - 大查询 : Divided by sum of values in column to find the ratio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com