By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE test (val1 INT, val2 INT);
INSERT INTO test VALUES (4,5), (2,5), (3,4), (4,1), (2,3);
SELECT * FROM test;
Records: 5 Duplicates: 0 Warnings: 0
val1 | val2 |
---|---|
4 | 5 |
2 | 5 |
3 | 4 |
4 | 1 |
2 | 3 |
SELECT value,
SUM(value = val1) total_1,
SUM(value = val2) total_2
FROM ( SELECT val1 value FROM test
UNION
SELECT val2 FROM test ) total
CROSS JOIN test
GROUP BY value
ORDER BY value;
value | total_1 | total_2 |
---|---|---|
1 | 0 | 1 |
2 | 2 | 0 |
3 | 1 | 1 |
4 | 2 | 1 |
5 | 0 | 2 |