Say I have a SQL db with multiple tables, and each of these tables has a column code
. What I want to produce is a table showing the number of codes shared between each pair of code
columns. I.e. a pairwise intersection count plot:
table1 table2 table3 table4
table1 10 2 3 5
table2 2 10 4 1
table3 3 4 10 2
table4 5 1 2 10
I can get each value individually using e.g.
WITH abc AS
(
SELECT code FROM table1
INTERSECT
SELECT code FROM table2
)
SELECT COUNT(*) AS table1Xtable2 FROM abc
But is there a query that will generate the entire output as desired as a table?