2013-06-05 68 views
6

我正在對數據集執行GROUP BY和COUNT(*),並且我想計算每個組在總數上的百分比。使用GROUP BY計算組的百分比

例如,在此查詢,我想知道有多少計數()每個國家代表在總(從publicdata SELECT COUNT():samples.natality):

SELECT state, count(*) 
FROM [publicdata:samples.natality] 
GROUP by state 

有幾種方法可以在SQL中完成,但我還沒有找到在BigQuery中實現它的方法,有誰知道嗎?

謝謝!

+1

檢查RATIO_TO_REPORT,最近宣佈的窗口函數(例如在回答中) –

回答

12

檢查RATIO_TO_REPORT,最近宣佈窗口功能之一:

SELECT state, ratio * 100 AS percent FROM (
SELECT state, count(*) AS total, RATIO_TO_REPORT(total) OVER() AS ratio 
FROM [publicdata:samples.natality] 
GROUP by state 
) 

state percent 
AL  1.4201828131159113 
AK  0.23521048665998198 
AZ  1.3332896746620975 
AR  0.7709591206172346 
CA  10.008298605982642 
+0

剛剛從昨天看到帖子中提到的新功能,這正是我想要的,謝謝! – inaki

+0

有沒有辦法將ROUND與RATIO_TO_REPORT一起使用?或者使用* 100計算百分比?我在第1行第37列出現「遇到」「覆蓋」「錯誤」。期待:「)」 – tinkerr

3

您可以使用虛擬值作爲關鍵字,對總數進行自聯接。例如:

SELECT 
    t1.state AS state, 
    t1.cnt AS cnt, 
    100 * t1.cnt/t2.total as percent 
FROM (
    SELECT 
    state, 
    COUNT(*) AS cnt, 
    1 AS key 
    FROM 
    [publicdata:samples.natality] 
    WHERE state is not null 
    GROUP BY 
    state) AS t1 
JOIN (
    SELECT 
    COUNT(*) AS total, 
    1 AS key 
    FROM 
    [publicdata:samples.natality]) AS t2 
ON t1.key = t2.key 
ORDER BY percent DESC 
+0

非常感謝喬丹,這正是我想要什麼! – inaki