在 Snowflake 中从 VARCHAR 创建嵌套数组

Creating Nested Array from VARCHAR in Snowflake

我在将结构化 VARCHAR 转换为 Snowflake 中的嵌套数组时遇到问题。

每个 VARCHAR 的格式为:

[['item1a', 'item1b', 'item1c', 'item1d'], ['item2a', 'item2b', 'item2c', 'item2d'], ..., ['itemNa', 'itemNb', 'itemNc', 'itemNd']] 

其中外层数组中有 N 个任意数组(长度均为 4)。

有人可以给我一些关于我用于此的 SQL 的提示吗?我还希望摆脱每个元素周围的单引号。

ARRAY_CONSTRUCT 是制作静态数组的正常方法。

SELECT 
    ARRAY_CONSTRUCT('item1a', 'item1b', 'item1c', 'item1d') as array1,
    ARRAY_CONSTRUCT('item2a', 'item2b', 'item2c', 'item2d') as array2,
    ARRAY_CONSTRUCT(array1, array1) as array_array_1,
    ARRAY_CONSTRUCT(ARRAY_CONSTRUCT('item1a', 'item1b', 'item1c', 'item1d'), ARRAY_CONSTRUCT('item2a', 'item2b', 'item2c', 'item2d')) as array_array_1;

给出:

ARRAY1 ARRAY2 ARRAY_ARRAY_1 ARRAY_ARRAY_1
[ "item1a", "item1b", "item1c", "item1d" ] [ "item2a", "item2b", "item2c", "item2d" ] [ [ "item1a", "item1b", "item1c", "item1d" ], [ "item1a", "item1b", "item1c", "item1d" ] ] [ [ "item1a", "item1b", "item1c", "item1d" ], [ "item2a", "item2b", "item2c", "item2d" ] ]

您可以通过 ARRAY_AGG:

从数据动态构建它们
SELECT 
    outer, 
    ARRAY_AGG(val) WITHIN GROUP ( ORDER BY inner ) as array
FROM (
    SELECT * from values
        (1, 1, 'item1a'),
        (1, 2, 'item1b'),
        (1, 3, 'item1c'),
        (2, 1, 'item2a'),
        (2, 2, 'item2b'),
        (2, 3, 'item2c')
        v(outer,inner, val)
)
GROUP BY 1 ORDER BY 1;

给出:

OUTER ARRAY
1 [ "item1a", "item1b", "item1c" ]
2 [ "item2a", "item2b", "item2c" ]

然后再次总结:

SELECT ARRAY_AGG(array) WITHIN GROUP ( ORDER BY outer )
FROM (
    SELECT 
        outer, 
        ARRAY_AGG(val) WITHIN GROUP ( ORDER BY inner ) as array
    FROM (
        SELECT * from values
            (1, 1, 'item1a'),
            (1, 2, 'item1b'),
            (1, 3, 'item1c'),
            (2, 1, 'item2a'),
            (2, 2, 'item2b'),
            (2, 3, 'item2c')
            v(outer,inner, val)
    )
    GROUP BY 1 
);

给出:

ARRAY_AGG(ARRAY) WITHIN GROUP ( ORDER BY OUTER )
[ [ "item1a", "item1b", "item1c" ], [ "item2a", "item2b", "item2c" ] ]