I have a table that is configured as follows:
+----+-------+-------+ | id | col1 | col2 | +----+-------+-------+ | 1 | John | Mike | | 2 | Mike | John | | 3 | Marty | John | | 4 | Walt | Marty | | 5 | Walt | Mike | +----+-------+-------+
I basically want to count unique values in both col1 and col2 and display them together with the corresponding unique value. The problem is that col1 does not necessarily contain all the same names as col2, and vice versa. I want to configure it as follows:
+-------+-------+------+ | names | col1 | col1 | +-------+-------+------+ | John | 1 | 2 | | Marty | 1 | 1 | | Mike | 1 | 2 | | Walt | 2 | NULL | +-------+-------+------+
I can independently select these values using:
SELECT col1, count(col1) as count FROM example GROUP BY col1;
OR
SELECT col2, count(col2) as count FROM example GROUP BY col2;
But it’s hard for me to understand how I join these two calculations, especially because the meaning of “Walt” here does not appear in col2.
source share