红移。将逗号分隔的值转换为包含所有组合的行

时间:2017-07-10 09:12:16

标签: amazon-redshift

我有:

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      
....

1 个答案:

答案 0 :(得分: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