Redshift create all the combinations of any length for the values in one column

好久不见. 提交于 2019-12-11 12:06:04

问题


How can we create all the combinations of any length for the values in one column and return the distinct count of another column for that combination?

Table:

+------+--------+
| Type |  Name  |
+------+--------+
| A    | Tom    |
| A    | Ben    |
| B    | Ben    |
| B    | Justin |
| C    | Ben    |
+------+--------+

Output Table:

+-------------+-------+
| Combination | Count |
+-------------+-------+
| A           |     2 |
| B           |     2 |
| C           |     1 |
| AB          |     3 |
| BC          |     2 |
| AC          |     2 |
| ABC         |     3 |
+-------------+-------+

When the combination is only A, there are Tom and Ben so it's 2.

When the combination is only B, 2 distinct names so it's 2.

When the combination is A and B, 3 distinct names: Tom, Ben, Justin so it's 3.

I'm working in Amazon Redshift. Thank you!


回答1:


NOTE: This answers the original version of the question which was tagged Postgres.

You can generate all combinations with this code

with recursive td as (
      select distinct type
      from t
     ),
     cte as (
      select td.type, td.type as lasttype, 1 as len
      from td
      union all
      select cte.type || t.type, t.type as lasttype, cte.len + 1
      from cte join
           t
           on 1=1 and t.type > cte.lasttype
     )

You can then use this in a join:

with recursive t as (
      select *
      from (values ('a'), ('b'), ('c'), ('d')) v(c)
     ),
     cte as (
      select t.c, t.c as lastc, 1 as len
      from t
      union all
      select cte.type || t.type, t.type as lasttype, cte.len + 1
      from cte join
           t
           on 1=1 and t.type > cte.lasttype
     )
select type, count(*)
from (select name, cte.type, count(*)
      from cte join
           t
           on cte.type like '%' || t.type || '%'
      group by name, cte.type
      having count(*) = length(cte.type)
     ) x
group by type
order by type;



回答2:


There is no way to generate all possible combinations (A, B, C, AB, AC, BC, etc) in Amazon Redshift.

(Well, you could select each unique value, smoosh them into one string, send it to a User-Defined Function, extract the result into multiple rows and then join it against a big query, but that really isn't something you'd like to attempt.)

One approach would be to create a table containing all possible combinations — you'd need to write a little program to do that (eg using itertools in Python). Then, you could join the data against that reasonably easy to get the desired result (eg IF 'ABC' CONTAINS '%A%').



来源:https://stackoverflow.com/questions/50417666/redshift-create-all-the-combinations-of-any-length-for-the-values-in-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!