2016-02-13 59 views
2

我試圖執行以下查詢:使用谷歌BigQuery的逗號作爲UNION ALL與IN子句

SELECT 
    author, link_id, COUNT(link_id) as cnt 
FROM 
    [fh-bigquery:reddit_comments.2015_12], 
    [fh-bigquery:reddit_comments.2015_11] 
WHERE link_id IN (
    SELECT posts.name 
    FROM [fh-bigquery:reddit_posts.full_corpus_201512] AS posts 
    WHERE posts.subreddit = 'politics' 
    ORDER BY posts.created_utc DESC 
    LIMIT 300 
) 
GROUP BY author, link_id 
ORDER BY author 

我在執行時收到此錯誤信息:JOIN(包括半連接)和UNION ALL(逗號,日期範圍)可能不會合併到一個SELECT語句中。將UNION ALL移至內部查詢或將JOIN移至外部查詢。

刪除其中一個comments表格可以正常工作,但我似乎無法弄清楚BigQuery的Comma as UNION ALL的工作原理。我試圖將聯合移動到內部查詢,但我仍然得到相同的錯誤。

回答

2

錯誤是我誤解將UNION ALL移動到內部查詢。解決錯誤,我不得不把這兩個表放在一個基本的select * from ...。工作查詢如下:

SELECT 
    author, link_id, COUNT(link_id) as cnt 
FROM (
    SELECT * 
    FROM 
    [fh-bigquery:reddit_comments.2015_12], 
    [fh-bigquery:reddit_comments.2015_11] 
) 
WHERE link_id IN (
    SELECT posts.name 
    FROM [fh-bigquery:reddit_posts.full_corpus_201512] AS posts 
    WHERE posts.subreddit = 'politics' 
    ORDER BY posts.created_utc DESC 
    LIMIT 300 
) 
GROUP BY author, link_id 
ORDER BY author