【问题标题】:How can I run a Presto query find the index of an element in an array of rows如何运行 Presto 查询查找行数组中元素的索引
【发布时间】:2022-01-09 11:05:30
【问题描述】:

我在 Presto 表中有以下列

sku product
012345 ['(productsku='012345', title='this title', ...)']

其中产品列是行数组

如何编写 SQL 查询来查找产品列表中 sku 的索引(例如 productListPosition)?

【问题讨论】:

    标签: sql google-analytics presto


    【解决方案1】:

    一种方法是将行数组转换为 productskus 数组并使用array_position

    WITH dataset(sku, product) AS (
        values
        (1, array[cast(ROW(1) as ROW(productsku INTEGER)),cast(ROW(2) as ROW(productsku INTEGER))]),
        (2, array[cast(ROW(1) as ROW(productsku INTEGER)),cast(ROW(2) as ROW(productsku INTEGER))]),
        (3, array[cast(ROW(1) as ROW(productsku INTEGER)),cast(ROW(2) as ROW(productsku INTEGER))])
    )
    
    select array_position(transform(product, r -> r.productsku), sku)
    from dataset
    

    输出:

    _col0
    1
    2
    0

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 1970-01-01
      • 2021-09-08
      • 2016-06-09
      • 2020-06-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      相关资源
      最近更新 更多