Skip to content

Instantly share code, notes, and snippets.

@vdt
Forked from Bemmu/gist:da5c79e9027bd3e248b7
Created February 16, 2016 09:51
Show Gist options
  • Select an option

  • Save vdt/d724fe49818069bd0305 to your computer and use it in GitHub Desktop.

Select an option

Save vdt/d724fe49818069bd0305 to your computer and use it in GitHub Desktop.
BigQuery for most popular countries on Hacker News
SELECT
b.country,
SUM(score) as total_score,
SUM(a.c) as mention_count,
ROUND(SUM(score) / SUM(a.c), 2) as avg_score
FROM
(
SELECT
a.word,
SUM(a.score) AS score,
COUNT(1) AS c
FROM (
SELECT
SPLIT(REGEXP_REPLACE(title, '[^a-zA-Z ]', ''), ' ') AS word,
score
FROM
[fh-bigquery:hackernews.stories]) a
GROUP BY
a.word
) a
JOIN
[countries.country] b
ON
a.word = b.aka
GROUP BY b.country
HAVING mention_count >= 100
ORDER BY avg_score DESC
LIMIT 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment