Search code examples
phpsqlarrayssnowflake-cloud-data-platformarray-difference

Finding the Difference By Row Between 2 Columns that are Both Arrays in SnowSql


I have a dataset that is comprised of a date and two other columns that are in array format. I am trying to find all the values in array_1 that are not in array_2.

Date | Array_1     | Array_2
-------------------------
1/20 | [1,2,3]     | [1,2]
2/20 | [4,5,6]     | [[1,2,4]

Desired Output:

    Date | Array_1     
    --------------
    1/20 | [3]    
    2/20 | [5,6]  

Solution

  • Just to remind that if you were to use application code for this, it might be as simple as (using PHP for this example):

    $array1 = array(4,5,6);
    $array2 = array(5,6,7);
    
    print_r(array_diff($array1, $array2));
    

    Outputs: Array ( [0] => 4 )