如何在BigQuery的标准SQL中实现RATIO_TO_REPORT()?

时间:2016-11-30 00:46:51

标签: google-bigquery

我有一个使用RATIO_TO_REPORT()的遗留SQL查询 - 它不使用开放访问表,但这就是它的样子:

SELECT
  Mutation_AA,
  Gene_name,
  CaseCount,
  RATIO_TO_REPORT(CaseCount) OVER (PARTITION BY Gene_name) AS ratio
FROM (
  SELECT
    COUNT(DISTINCT ID_tumour, 50000) AS CaseCount,
    Mutation_AA,
    Gene_name
  FROM
    [isb-cgc:COSMIC.grch38_v79]
  GROUP BY
    Mutation_AA,
    Gene_name )

我正在尝试从旧版SQL迁移到标准SQL(在使用BigQuery之前从未使用过SQL),因此我们非常感谢提示! THX

2 个答案:

答案 0 :(得分:5)

直接计算比率:

SELECT Mutation_AA,
       Gene_name,
       CaseCount,
       (CaseCount / SUM(CaseCount) OVER (PARTITION BY Gene_name)) AS ratio
. . .

您不需要子查询:

SELECT Mutation_AA, Gene_name,
       COUNT(DISTINCT ID_tumour, 50000) AS CaseCount,
       COUNT(DISTINCT ID_tumour, 50000) / SUM(COUNT(DISTINCT ID_tumour, 50000)) OVER (PARTITION BY Gene_Name) as ratio
FROM [isb-cgc:COSMIC.grch38_v79]
GROUP BY Mutation_AA, Gene_name ;

答案 1 :(得分:0)

或者使用其中一个BigQuery公共数据集的简单示例:

select state, (state_count / total) as ratio
from (
  SELECT state, count(*) AS state_count, sum(count(*)) OVER() AS total
  FROM `bigquery-public-data.samples.natality` 
  GROUP by state
) s