Skip to content

Instantly share code, notes, and snippets.

@savelee
Created February 22, 2017 09:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save savelee/c42abc2920aaedf482dbe1cfe79a76c0 to your computer and use it in GitHub Desktop.
Save savelee/c42abc2920aaedf482dbe1cfe79a76c0 to your computer and use it in GitHub Desktop.
BigQuery Array Struct example
#standardSQL
#Top two Hacker News articles by day
WITH TitlesAndScores AS (
SELECT
ARRAY_AGG(STRUCT(title,
score)) AS titles,
EXTRACT(DATE
FROM
time_ts) AS date
FROM
`bigquery-public-data.hacker_news.stories`
WHERE
score IS NOT NULL
AND title IS NOT NULL
GROUP BY
date)
SELECT
date,
ARRAY(
SELECT
AS STRUCT title,
score
FROM
UNNEST(titles)
ORDER BY
score DESC
LIMIT
2) AS top_articles
FROM
TitlesAndScores;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment