sqldatabasesnowflake-cloud-data-platformsnowflake-schema

how to test if row values in source when taken as columns in target


I have a source table like below,

ID Column value
1 apple 10
1 apple 8
1 banana 9
1 banana 12

I have a target table like below,

ID apple banana
1 10 9
1 8 12

How do I test/verify this using snowflake


Solution

  • with src_data(id, col, val) as (
        select * from values
            (1  ,'apple',   10),
            (1  ,'apple',   8),
            (1  ,'banana',  9),
            (1  ,'banana',  12)
    ), target_data(id, apple, banana) as (
        select * from values
            (1, 10, 9),
            (1, 8,  12),
            (2, 10, 9),
            (1, 10, 13),
            (1, 7, 12)
    )
    select t.*
        ,a.val as a_val
        ,b.val as b_val
    from target_data as t
    left join src_data as a
        on t.id = a.id 
            and a.col = 'apple'
            and t.apple = a.val 
    left join src_data as b
        on t.id = b.id 
            and b.col = 'banana'
            and t.banana = b.val                
    

    shows how to join to the data using LEFT JOIN that allow misses:

    enter image description here

    so now we just change those to INNER JOINS, and only output the t.* values:

    select t.*
    from target_data as t
    join src_data as a
        on t.id = a.id 
            and a.col = 'apple'
            and t.apple = a.val 
    join src_data as b
        on t.id = b.id 
            and b.col = 'banana'
            and t.banana = b.val 
    

    and we get filtered results:

    ID APPLE BANANA
    1 10 9
    1 8 12