1

I'm having trouble casting a structured VARCHAR to a nested array in Snowflake.

Each VARCHAR has the format:

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

where there are N arbitrary arrays (all of length 4) in the outer array.

Can someone give me a hint on the SQL I'd used for this? I'd also ideally like to get rid of the single quotes surrounding each element.

1 Answer 1

1

ARRAY_CONSTRUCT is the normal way to make static arrays.

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;

gives:

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" ] ]

OR

you can build them from data dynamically via 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;

gives:

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

and then wrapped up again:

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 
);

gives:

ARRAY_AGG(ARRAY) WITHIN GROUP ( ORDER BY OUTER )
[ [ "item1a", "item1b", "item1c" ], [ "item2a", "item2b", "item2c" ] ]
Sign up to request clarification or add additional context in comments.

4 Comments

This is not exactly what I was looking for, but parts of it were useful. I wound up having to some tricky string and array manipulation calls to get it in the shape I needed. Appreciate the fast response though!
@sethkim3 perhaps you could post the solution you used in the end, and examples of the input data you had/used and the output results you got/wanted.
It's probably not going to be useful on its own. There are a number of data cleansing methods used that do not pertain directly to the question. But the general procedure was to split the data, flatten it, clean it, then aggregate it twice to build the final array.
So my example showing how to build a double arrays pretty much answered the question "how do a build nested arrays" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.