Skip to content
Advertisement

Join on Id in Variant (Array) Snowflake

I have two tables t1 and t2 created as follows:

Create Tables

Goal

I am looking to join t2 to t1 if the id of the record exists in the t1 variant array.

What I’ve Tried

I came across the ARRAY_CONTAINS function which looked perfect. But with the following I am receiving no results:

How Do I Get This?

Any suggestions on how to achieve the following result?

Desired Result

Advertisement

Answer

So to debug what’s happening I added more and more line to the below SQL, but the short story is you have the parameters to ARRAY_CONTAINS back to front:

gives:

ID NAME ID IDS_ARRAY CONST_1 CONST_2 V1 IT_WORKS
1 test 1 [ “1”, “2” ] TRUE TRUE “1” TRUE
1 test2 2 [ “1”, “2” ] TRUE TRUE “2” TRUE

so now using the correct form:

we get:

ID NAME
1 test
1 test2
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement