Возникли проблемы при объединении нескольких таблиц Reddit с предложением AS и ON - PullRequest
0 голосов
/ 28 января 2019

Я пытаюсь присоединить комментарии к сообщениям для нескольких таблиц.Мне нужно предложение AS, потому что таблица записей и таблица комментариев имеют общий столбец «оценка».

Моя цель - найти верхние комментарии в верхних сообщениях с данными во всех этих таблицах.

#standardSQL
SELECT posts.title, posts.url, posts.score AS postsscore, 
DATE_TRUNC(DATE(TIMESTAMP_SECONDS(posts.created_utc)), MONTH), 
comments.body, comments.score AS commentsscore, comments.id

FROM

fh-bigquery.reddit_posts.2015_12, fh-bigquery.reddit_posts.2016_01, fh-bigquery.reddit_posts.2016_02,fh-bigquery.reddit_posts.2016_03, fh-bigquery.reddit_posts.2016_04, fh-bigquery.reddit_posts.2016_05, fh-bigquery.reddit_posts.2016_06, fh-bigquery.reddit_posts.2016_07, fh-bigquery.reddit_posts.2016_08, fh-bigquery.reddit_posts.2016_09, fh-bigquery.reddit_posts.2016_10, fh-bigquery.reddit_posts.2016_11, fh-bigquery.reddit_posts.2016_12, fh-bigquery.reddit_posts.2017_01, fh-bigquery.reddit_posts.2017_02, fh-bigquery.reddit_posts.2017_03, fh-bigquery.reddit_posts.2017_04, fh-bigquery.reddit_posts.2017_05, fh-bigquery.reddit_posts.2017_06, fh-bigquery.reddit_posts.2017_07, fh-bigquery.reddit_posts.2017_08, fh-bigquery.reddit_posts.2017_09, fh-bigquery.reddit_posts.2017_10, fh-bigquery.reddit_posts.2017_11, fh-bigquery.reddit_posts.2017_12, fh-bigquery.reddit_posts.2018_01, fh-bigquery.reddit_posts.2018_02, fh-bigquery.reddit_posts.2018_03,fh-bigquery.reddit_posts.2018_04, fh-bigquery.reddit_posts.2018_05, fh-bigquery.reddit_posts.2018_06, fh-bigquery.reddit_posts.2018_07, fh-bigquery.reddit_posts.2018_08, fh-bigquery.reddit_posts.2018_09, fh-bigquery.reddit_posts.2018_10

AS posts

JOIN

fh-bigquery.reddit_comments.2015_12, fh-bigquery.reddit_comments.2016_01, fh-bigquery.reddit_comments.2016_02, fh-bigquery.reddit_comments.2016_03, fh-bigquery.reddit_comments.2016_04, fh-bigquery.reddit_comments.2016_05, fh-bigquery.reddit_comments.2016_06, fh-bigquery.reddit_comments.2016_07, fh-bigquery.reddit_comments.2016_08, fh-bigquery.reddit_comments.2016_09, fh-bigquery.reddit_comments.2016_10, fh-bigquery.reddit_comments.2016_11, fh-bigquery.reddit_comments.2016_12, fh-bigquery.reddit_comments.2017_01, fh-bigquery.reddit_comments.2017_02, fh-bigquery.reddit_comments.2017_03,fh-bigquery.reddit_comments.2017_04, fh-bigquery.reddit_comments.2017_05, fh-bigquery.reddit_comments.2017_06, fh-bigquery.reddit_comments.2017_07, fh-bigquery.reddit_comments.2017_08, fh-bigquery.reddit_comments.2017_09, fh-bigquery.reddit_comments.2017_10, fh-bigquery.reddit_comments.2017_11, fh-bigquery.reddit_comments.2017_12, fh-bigquery.reddit_comments.2018_01, fh-bigquery.reddit_comments.2018_02, fh-bigquery.reddit_comments.2018_03, fh-bigquery.reddit_comments.2018_04, fh-bigquery.reddit_comments.2018_05, fh-bigquery.reddit_comments.2018_06, fh-bigquery.reddit_comments.2018_07, fh-bigquery.reddit_comments.2018_08, fh-bigquery.reddit_comments.2018_09, fh-bigquery.reddit_comments.2018_10

AS comments

ON posts.id = SUBSTR(comments.link_id, 4)

WHERE posts.subreddit = 'Showerthoughts' AND posts.score >100 AND comments.score >100
ORDER BY posts.score DESC

Моя цель - найти верхние комментарии в верхних сообщениях сданные во всех этих таблицах.

1 Ответ

0 голосов
/ 28 января 2019

Хорошо, поэтому проблемы с этим запросом:

  • Будьте осторожны!Этот запрос обработает много данных.Я мог бы повторно кластеризовать таблицы, чтобы сделать этот способ более эффективным, но я этого еще не сделал.
  • В #standardSQL запятая означает JOIN, а не UNION.Таким образом, вам нужно UNION таблиц.
  • Ярлык: вы можете добавить * в конце имени таблицы, чтобы развернуть ее во все соответствующие таблицы.
  • Используйте обратную галочку дляэкранируйте имена таблиц.

С учетом сказанного рабочий запрос будет выглядеть следующим образом:

#standardSQL
SELECT posts.title, posts.url, posts.score AS postsscore, 
DATE_TRUNC(DATE(TIMESTAMP_SECONDS(posts.created_utc)), MONTH), 
SUBSTR(comments.body, 0, 80), comments.score AS commentsscore, comments.id

FROM `fh-bigquery.reddit_posts.2015*` AS posts
JOIN `fh-bigquery.reddit_comments.2015*` AS comments

ON posts.id = SUBSTR(comments.link_id, 4)

WHERE posts.subreddit = 'Showerthoughts' 
AND posts.score >100 
AND comments.score >100
ORDER BY posts.score DESC
...