2016-11-30 84 views
3

我有一個使用RATIO_TO_REPORT()遺留的SQL查詢 - 它不使用開放式訪問表,但是這是什麼樣子:如何在BigQuery中的標準SQL中實現RATIO_TO_REPORT()?

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

回答

3

只需直接計算的比例:

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 ; 
相關問題