Skip to content
Advertisement

How to find the two combinations of elements that are present the most in all the groups? [closed]

For example in this simple table:

Group Element
xxx a
xxx b
xxx c
xxx d
xxx e
yyy a
yyy b
yyy f
yyy g
zzz a
zzz b
zzz c
zzz g

‘a’ and ‘b’ are the combination of elements present the most in all groups

Advertisement

Answer

You can do it with a triangle join

with c1 as (
  select t1.grp, t1.Element e1, t2.Element e2
  from tbl t1
  join tbl t2 on t1.grp = t2.grp and t1.Element < t2.Element 
)
  select e1,e2
  from c1
  group by e1,e2
  order by count(*) desc
  limit 1
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement