【问题标题】:Google BigQuery: position of element in arrayGoogle BigQuery:数组中元素的位置
【发布时间】: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


    【解决方案1】:

    这可以使用with offset:

    WITH data_table AS
    (SELECT '{"fruit":[{"apples":5,"oranges":10},{"apples":2,"oranges":4}]}' AS data)
    
    SELECT fruit, i
    FROM data_table, UNNEST(JSON_EXTRACT_ARRAY(  data,  '$.fruit')) as fruit with offset as i 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 2019-02-06
    相关资源
    最近更新 更多