【发布时间】:2020-06-21 12:15:37
【问题描述】:
是否可以在 Google BigQuery 中获取元素在数组中的位置? 类似 Postgresql 中的 array_postion 函数?
特别是我需要在未嵌套数组之后获取元素的数组位置:
WITH data_table AS
(SELECT '{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}]}' AS data)
SELECT fruit, ? as position
FROM data_table, UNNEST(JSON_EXTRACT_ARRAY( data, '$.fruit')) as fruit
fruit | position
-------------------------------------
{"apples":5,"oranges":10} | 1
{"apples":2,"oranges":4} | 2
我尝试使用 row_number() 解决方法,但我不确定是否排序
WITH data_table AS
(SELECT '{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}]}' AS data)
SELECT fruit, ROW_NUMBER() OVER() AS position
FROM data_table, UNNEST(JSON_EXTRACT_ARRAY( data, '$.fruit')) as fruit
【问题讨论】:
标签: arrays json sorting google-bigquery