Created
February 22, 2017 09:43
-
-
Save savelee/c42abc2920aaedf482dbe1cfe79a76c0 to your computer and use it in GitHub Desktop.
BigQuery Array Struct example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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