Creating Group for multiple columns with multiple duplicates

雨燕双飞 提交于 2021-02-11 14:41:52

问题


The below table contains details of members and their policies. We need to form a group of 2 or more members if they have at least one policy in common.

Member_ID   Policy
101         X1
103         Y2
104         Z1
101         Y1
102         X1
101         X3
103         Z1
101         X2
102         Y3
105         Y1

Required result:

GROUP   Member_ID   Policy
1       101         X1
1       101         X2
1       101         X3
1       101         Y1
1       102         X1
1       102         Y3
1       105         Y1
2       103         Y2
2       103         Z1
2       104         Z1

回答1:


What you describe is a graph-walking algorithm. This is complicated because you may need to traverse multiple levels. For instance:

101    X1
102    Y1
102    Y2
103    Y2
103    Z1
104    Z1

These are all in the same group by 101 and 104 have nothing in common.

Recursive CTEs can handle this. For this problem, it looks like:

with edges as (
      select distinct t.memberid as m1, t2.memberid as m2
      from t join
           t t2
           on t2.policy = t.policy -- and t2.memberid <> t.memberid
     ),
     cte as (
      select m1, m2, convert(varchar(max), concat(',', m1, ',', m2, ',')) as members, 1 as lev
      from edges
      union all
      select cte.m1, e.m2, concat(members, e.m2, ','), lev + 1
      from cte join
           edges e
           on cte.m2 = e.m1
      where members not like concat('%,', e.m2, ',%') 
     )
select m1, dense_rank() over (order by min(m2)) as grouping
from cte
group by m1;

Here is a db<>fiddle.



来源:https://stackoverflow.com/questions/60771198/creating-group-for-multiple-columns-with-multiple-duplicates

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