2017-01-23 72 views
1

使用大查詢彙總和分析彙總,我想查詢

計數(不同山坳),計數(不同山坳)以上(分區由COL2
查詢都在同一個大查詢的查詢

但我收到一個意外的錯誤。

下面是查詢我想要執行的模板:

SELECT 
      country, 
      partner, 
      segment_id, 
      COUNT(DISTINCT pv_id) pvs, 
      COUNT(DISTINCT pv_id) over(PARTITION BY country) country_total_pvs 
FROM (...) 
GROUP BY 
      country, 
      partner, 
      segment_id 

而且我不斷收到錯誤:

Error: Expression 'pv_id' is not present in the GROUP BY list

沒有第5列(分析計數),該查詢執行時沒有任何錯誤。

想法?

非常感謝!

+0

樣本數據和預期的結果可以解釋你想做的事。正如所寫,該查詢無效,因此它不能真正表達您的意圖。 –

+0

在這裏,你可以採取下面的代碼,例如:
'選擇 語言, 標題, COUNT(DISTINCT contirbutor_username), COUNT(DISTINCT contirbutor_username)以上( PARTITION BY 語言) FROM [BigQuery的公開數據:samples.wikipedia] GROUP BY language, title'我真正想要的是對不同維度的結果進行分組。 –

回答

1

以下應該工作,但我猜不會做你想要的:

SELECT country, partner, segment_id, 
     COUNT(DISTINCT pv_id) pvs, 
     SUM(COUNT(DISTINCT pv_id)) OVER (PARTITION BY country) as country_total_pvs 
FROM (...) q 
GROUP BY country, partner, segment_id; 

相反:

SELECT country, partner, segment_id, 
     COUNT(DISTINCT pv_id) pvs, 
     country_total_pvs 
FROM (SELECT q.*, 
      COUNT(DISTINCT pv_id) OVER (PARTITION BY country) as country_total_pvs 
     FROM (...) q 
    ) q 
GROUP BY country, partner, segment_id, country_total_pvs; 
0

嘗試以下

SELECT 
    a.country AS country, 
    partner, 
    segment_id, 
    COUNT(DISTINCT pv_id) pvs, 
    country_total_pvs 
FROM youTable AS a 
LEFT JOIN (
    SELECT country, COUNT(DISTINCT pv_id) country_total_pvs 
    FROM youTable GROUP BY country 
) AS b 
ON a.country = b.country 
GROUP BY 
    country, 
    partner, 
    segment_id, 
    country_total_pvs 

有想法 - 以上將爲BigQuery StandardSQL提供「正確」不同的計數,但是在BigQuery Legacy SQL COUNT(DISTINCT)是一個統計近似值,不能保證準確。您可以使用EXACT_COUNT_DISTINCT代替

下面稍微優化版本

SELECT 
    a.country AS country, 
    partner, 
    segment_id, 
    pvs, 
    country_total_pvs 
FROM (
    SELECT 
    country, 
    partner, 
    segment_id, 
    COUNT(DISTINCT pv_id) pvs 
    FROM youTable 
    GROUP BY country, partner, segment_id 
) AS a 
LEFT JOIN (
    SELECT country, 
    COUNT(DISTINCT pv_id) country_total_pvs 
    FROM youTable GROUP BY country 
) AS b 
ON a.country = b.country