有没有办法在Google BigQuery重复字段中选择前n个项?
查看Google文档中的示例:
WITH items AS
(SELECT ["apples", "bananas", "pears", "grapes"] as list
UNION ALL
SELECT ["coffee", "tea", "milk" ] as list
UNION ALL
SELECT ["cake", "pie"] as list)
SELECT list, list[OFFSET(0)] as offset_1, list[OFFSET(1)] as offset_2
FROM items;
给出以下内容:
OFFSET()函数返回单个(非重复)元素。
有没有一种优雅的方法可以在重复字段中选择前n个(比如说n = 2个)元素?查看第1行,输出应该是带有两个元素的重复字段:苹果和香蕉。
答案 0 :(得分:5)
以下是BigQuery Standard SQL
#standardSQL
WITH items AS (
SELECT ["apples", "bananas", "pears", "grapes"] AS list UNION ALL
SELECT ["coffee", "tea", "milk" ] AS list UNION ALL
SELECT ["cake", "pie"] AS list
)
SELECT
list AS original_list,
ARRAY(SELECT item FROM UNNEST(list) item WITH OFFSET pos WHERE pos < 2) new_list
FROM items
输出为
Row original_list new_list
1 apples apples
bananas bananas
pears
grapes
2 coffee coffee
tea tea
milk
3 cake cake
pie pie