With the following table
CREATE TABLE T1 (
A varchar(2),
B varchar(2)
);
INSERT INTO T1 VALUES
('aa', 'm'), ('aa', 'n'),
('bb', 'n'), ('bb', 'o'),
('cc', 'n'), ('cc', 'o'),
('dd', 'c'), ('dd', 'a'), ('dd', 'r'),
('ee', 'a'), ('ee', 'c'), ('ee', 'r')
A | B
----+----
aa | m
aa | n
bb | n
bb | o
cc | n
cc | o
dd | c
dd | a
dd | r
ee | a
ee | c
ee | r
How can I select and group the values in A that match all the coresponding values in B. For example bb and cc make up a group because they both contain ‘n’ and ‘o’.
So the result would be
Group | A
----------
1 | bb
1 | cc
2 | dd
2 | ee
Here’s one approach: it first calculates the matching "sets", where a set is a group of two
A‘s which match. Then it calculate the "head", or the lowestAfor sets in the same group. Usingdense_rankyou can number the heads, and then join back on the list of sets to create a list of all set members.Query at SE Data.