【问题标题】:Redshift. Convert comma delimited values into rows with all combinations红移。将逗号分隔的值转换为具有所有组合的行
【发布时间】:2017-12-13 22:50:37
【问题描述】:

我有:

user_id|user_name|user_action
-----------------------------
1      | Shone   | start,stop,cancell

我想看看:

user_id|user_name|parsed_action 
------------------------------- 
1      | Shone   | start 
1      | Shone   | start,stop 
1      | Shone   | start,cancell
1      | Shone   | start,stop,cancell       
1      | Shone   | stop         
1      | Shone   | stop,cancell
1      | Shone   | cancell      
....

【问题讨论】:

    标签: amazon-redshift


    【解决方案1】:

    您可以创建以下 Python UDF:

    create or replace function get_unique_combinations(list varchar(max))
    returns varchar(max)
    stable as $$
    
    from itertools import combinations
    
    arr = list.split(',')
    
    response = []
    
    for L in range(1, len(arr)+1):
        for subset in combinations(arr, L):
            response.append(','.join(subset))
    
    return ';'.join(response)
    
    $$ language plpythonu;
    

    这将获取您的操作列表并返回用分号分隔的唯一组合(组合中的元素本身将用逗号分隔)。然后您使用UNION hack 将值拆分为单独的行,如下所示:

    WITH unique_combinations as (
        SELECT 
         user_id
        ,user_name
        ,get_unique_combinations(user_actions) as action_combinations
        FROM your_table
    )
    ,unwrap_lists as (
        SELECT 
         user_id
        ,user_name
        ,split_part(action_combinations,';',1) as parsed_action
        FROM unique_combinations
        UNION ALL
        SELECT 
         user_id
        ,user_name
        ,split_part(action_combinations,';',2) as parsed_action
        FROM unique_combinations
        -- as much UNIONS as possible combinations you have for a single element, with the 3rd parameter (1-based array index) increasing by 1
        )
    SELECT *
    FROM unwrap_lists
    WHERE parsed_action is not null
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 1970-01-01
      • 2012-12-02
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多