Problem
I have three tables that are structured like so:
t1
id | count_1
--------------
1 | 9
2 | 4
3 | 3
t2
id | count_2
--------------
1 | 2
3 | 3
t3
id | count_3
--------------
1 | 1
4 | 8
id
is unique in each table. Note that not all ids occur in each table. Here is the SQL to create those tables if you'd like to test.
I'm trying to merge all those tables with a column for each count, defaulting to zero if there is no count for that particular id
. Like this:
id | count_1 | count_2 | count_3
----------------------------------
1 | 9 | 2 | 1
2 | 4 | 0 | 0
3 | 3 | 3 | 0
4 | 0 | 0 | 8
Attempt
I thought this was a natural use case for a full outer join, like this:
SELECT
COALESCE(t1.id, t2.id, t3.id) as id,
COALESCE(t1.count_1, 0) as count_1,
COALESCE(t2.count_2, 0) as count_2,
COALESCE(t3.count_3, 0) as count_3
FROM
t1
FULL OUTER JOIN t2
ON t1.id = t2.id
FULL OUTER JOIN t3
ON t1.id = t3.id
ORDER BY id ASC;
But this returns a result with non unique ids, where each row is just a row from one of the original tables with zeroes filling in the remaining columns:
id | count_1 | count_2 | count_3
----------------------------------
1 | 9 | 0 | 0 # <- should
1 | 0 | 2 | 0 # <- be
1 | 0 | 0 | 1 # <- one row
2 | 4 | 0 | 0
3 | 3 | 0 | 0 # <- should also be
3 | 0 | 3 | 0 # <- one row
4 | 0 | 0 | 8
Evidently I don't understand outer joins as well as I thought I did. Can anyone show me the correct way to do this?
id
= 5 in tables 2 and 3 only, shows the problem. @ypercubeᵀᴹ 's solution (COALESCE
the t1 and t2id
s) does resolve it.