我有两个SQL查询:
SELECT subreddit, count(subreddit) as count
FROM [fh-bigquery:reddit_comments.all]
where author="***********" GROUP by subreddit ORDER BY count DESC;
AND
SELECT subreddit, count(subreddit) as count
FROM [redditcollaborativefiltering:aggregate_comments.reddit_posts_all]
where author="***********" GROUP by subreddit ORDER BY count DESC;
我希望能够将这两个查询的结果加入到一个具有相同列的结果中,但是,计数相互相加。有没有简单的方法呢?
答案 0 :(得分:1)
您可以使用UNION ALL
和其他聚合:
SELECT subredit, SUM(cnt) as cnt
FROM ((SELECT subreddit, count(subreddit) as cnt
FROM [fh-bigquery:reddit_comments.all]
WHERE author = '***********'
GROUP BY subreddit
) UNION ALL
(SELECT subreddit, count(subreddit) as cnt
FROM [redditcollaborativefiltering:aggregate_comments.reddit_posts_all]
WHERE author = '***********'
GROUP by subreddit
)
) sc
GROUP BY subreddit
ORDER BY cnt DESC;
答案 1 :(得分:1)
对于BigQuery Legacy SQL(我在您的示例中看到您使用),您可以使用以下内容:
#legacySQL
SELECT subredit, SUM(cnt) as cnt
FROM (SELECT subreddit, COUNT(subreddit) as cnt
FROM [fh-bigquery:reddit_comments.all]
WHERE author = '***********'
GROUP BY subreddit
),
(SELECT subreddit, COUNT(subreddit) as cnt
FROM [redditcollaborativefiltering:aggregate_comments.reddit_posts_all]
WHERE author = '***********'
GROUP by subreddit
)
GROUP BY subreddit
ORDER BY cnt DESC
正如您在此处所见 - 旧版SQL中的逗号用作UNION ALL
以上可以进一步简化
#legacySQL
SELECT subreddit, COUNT(subreddit) as cnt
FROM [fh-bigquery:reddit_comments.all],
[redditcollaborativefiltering:aggregate_comments.reddit_posts_all]
WHERE author = '***********'
GROUP BY subreddit
ORDER BY cnt DESC
您可以阅读有关BigQuery Legacy SQL的Comma as UNION ALL
的更多信息