【问题标题】:how to split the data in snowflake and put them in respective columns如何拆分雪花中的数据并将它们放在相应的列中
【发布时间】:2023-04-01 11:10:01
【问题描述】:

示例:

id  core           primary         secondary
101 4355|6755     4355|7866 
102 8566|6755      8566              8566
```````````````````````````````````````````````````````````````````````````````

I need to split the data into another table. One with only codes and indicators. All the codes should be in one column under codes and their respective codes as an indicator in other columns as below:
``````````````````````````````````````````````````````````````````````````

id      codes       core_ind        primary_ind secondary_ind
101     4355        Y                   Y   
101     6755        Y           
101     7866                            Y   
102     8566        Y                   Y           Y
102     6755        Y           

我可以使用横向拆分列,但不确定如何将指标放在各自的列中。你能提出什么建议吗?以下是我用于拆分的代码。使用此代码,我可以将所有代码和 ID 放在各自的列下。现在,我需要把上面提到的指标放上去

SELECT id, c.value::varchar AS codes 
FROM table
   ,lateral flatten (input => split(core, '|')) c
UNION ALL 
SELECT id, d.value::varchar AS codes 
FROM  table 
   ,lateral flatten (input => split(primary, '|')) d
UNION ALL 
SELECT id, e.value::varchar AS codes 
FROM table
    ,lateral flatten (input => split(secondary, '|')) e

提前致谢!!

【问题讨论】:

    标签: sql join snowflake-cloud-data-platform flatten lateral


    【解决方案1】:

    并不是说我是工会的超级粉丝,现在让他们独自一人,它可以像这样工作:

    with data as (
        select * from values (101, '4355|6755', '4355|7866', NULL),
                             (102, '8566|6755', '8566'     , 8566)
             v(id, core, primary, secondary)
    ), exploded as (      
        SELECT id, 'c' as type, c.value::varchar AS codes 
        FROM data
           ,lateral flatten (input => split(core, '|')) c
        UNION ALL 
        SELECT id, 'p' as type, d.value::varchar AS codes 
        FROM  data 
           ,lateral flatten (input => split(primary, '|')) d
        UNION ALL 
        SELECT id, 's' as type, e.value::varchar AS codes 
        FROM data
            ,lateral flatten (input => split(secondary, '|')) e
    )
    select id
        ,codes
        ,iff(sum(iff(type='c',1,0))>0,'Y','') as core_ind
        ,iff(sum(iff(type='p',1,0))>0,'Y','') as primary_ind
        ,iff(sum(iff(type='s',1,0))>0,'Y','') as secondary_ind
    from exploded
    group by 1,2
    order by 1,2;
    

    这给出了:

    ID  CODES   CORE_IND   PRIMARY_IND   SECONDARY_IND
    101 4355    Y          Y    
    101 6755    Y       
    101 7866               Y    
    102 6755    Y       
    102 8566    Y          Y            Y
    

    所以这也可以用 UNPIVOT 然后 FLATTEN 来完成,感觉也好不了多少:

    with data as (
       select * from values (101, '4355|6755', '4355|7866', NULL),
                             (102, '8566|6755', '8566'     , '8566')
             v(id, data_a, data_b, data_c)
    ), unpivoted as (
        select * 
        from data unpivot(code for type in (data_a, data_b, data_c))
    ), flattened as (
        select id, type, f.value as codes
        from unpivoted
            ,lateral flatten (input => split(code, '|')) f
    )
    select id
        ,codes
        ,iff(sum(iff(type='DATA_A',1,0))>0,'Y','') as core_ind
        ,iff(sum(iff(type='DATA_B',1,0))>0,'Y','') as primary_ind
        ,iff(sum(iff(type='DATA_C',1,0))>0,'Y','') as secondary_ind
    from flattened
    group by 1,2
    order by 1,2;
    

    【讨论】:

    • 经过我的深思熟虑,工会是我能想到的唯一方法将它们分开,以允许在 ID 上重新分组。
    • 是的。我尝试了其他方法,但没有奏效。让我知道他们是否也有更好的方法。感谢您的帮助。
    • 只需通过 UNPIVOT 添加另一种方式,然后添加一个 FLATTEN,我怀疑在更大的数据集上,您的方式可能会执行得更好,并且联合可以并行发生
    猜你喜欢
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2016-01-05
    相关资源
    最近更新 更多