What I want is to create DISTINCT pairs of column 2 for each ID and arrange them by count.
Let's use this table as an example:
CREATE TABLE mytable
(`ID` int, `C2` varchar(1), `C3` varchar(2))
;
INSERT INTO mytable
(`ID`, `C2`, `C3`)
VALUES
(1, 'A',' a1'),
(1, 'B', 'b1'),
(2, 'A', 'a2'),
(3, 'A', 'a3'),
(3, 'C', 'c3'),
(3, 'A', 'a4'),
(4, 'A', 'a1'),
(4, 'B', 'b4'),
(4, 'A', 'a2'),
(4, 'D', 'd1');
For 1, pair would be A-B.
For 2, one wouldn't exist.
For 3, pair would be A-C.
For 4, pairs would be A-B, A-D, and B-D.
So the output would be:
| Pair | Cnt |
| A-B | 2 |
| A-C | 1 |
| A-D | 1 |
| B-D | 1 |
Is this something we can do in SQL using something like GROUP_CONCAT
?
I've been wrapping my head around this problem for days and still can't think of a simple solution.
Thanks!