Unnest first N items in a Google BigQuery Repeated Field

Is there a way to select the first n items in a Google BigQuery Repeated field?

\n\n

Below is for BigQuery Standard SQL

\n\n \n\n
#standardSQL\nWITH items AS (\n  SELECT ["apples", "bananas", "pears", "grapes"] AS list UNION ALL\n  SELECT ["coffee", "tea", "milk" ] AS list UNION ALL\n  SELECT ["cake", "pie"] AS list\n)\nSELECT \n  list AS original_list, \n  ARRAY(SELECT item FROM UNNEST(list) item WITH OFFSET pos WHERE pos < 2) new_list\nFROM items\n
\n\n

with output as

\n\n
Row original_list   new_list     \n1   apples          apples   \n    bananas         bananas  \n    pears        \n    grapes       \n2   coffee          coffee   \n    tea             tea  \n    milk         \n3   cake            cake     \n    pie             pie\n

Mike-Barn

posted on

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds