1

例子:

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

提前致谢!!

4

1 回答 1

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;
于 2020-05-06T00:51:15.100 回答