使用数组数据类型查询属性

问题描述 投票:0回答:1

我有这个数据类型的值

array<struct<id:string,name:string,values:array<string>> 
[{id=gid://test/1234,name=尺寸,values=[L、M、S、XS]},{id=gid://test/12345,name=颜色,values=[黑色]} ]

如何查询以获取像这样的输出的大小值 [L、M、S、XS]

我试过这个

SELECT 
   * 
FROM 
    mytable
CROSS JOIN UNNEST(options) AS t (option_struct)
CROSS JOIN UNNEST(option_struct.values) AS size_value
WHERE 
    option_struct.name = 'Size'
sql amazon-athena presto trino unnest
1个回答
0
投票

如果目标是获取数组,则不需要使用第二个

unnest

-- sample data
with dataset(options) as(
 values (array[
        cast(ROW('gid://test/1234', 'Size', array['L', 'M', 'S', 'XS']) as ROW(id varchar, name varchar, "values" array(varchar))),
        cast(ROW('gid://test/12345', 'Color', array['Black']) as ROW(id varchar, name varchar, "values" array(varchar)))
    ])
)

-- query
SELECT option_struct."values" as size_value
FROM
    dataset
    , UNNEST(options) AS option_struct
WHERE
    option_struct.name = 'Size';
© www.soinside.com 2019 - 2024. All rights reserved.