I have many tables (let's call them single-parameter-tables) which include ID (primary key) and another parameter (2 columns in each table). I wish to join all of them in a joined_table consisting ID and param_1, param_2, ...., param_n columns. The joined_table is NOT NULL for ID column (primary key) and Nullable for other columns. When the parameters share the ID value, I can do the FULL OUTER JOIN normally and there's no problem. But when one parameter doesn't share primary key with any of the other parameters, I face a problem. Simply speaking, assume for ID 124 there is some value for param_3 from the third single_param-table but no other occurrence and value in other single-parameter-tables.
My code is as follows:
Insert into [joined_table]
(ID, param_1,param_2,param_3)
SELECT
ID
,param1
,param2
,param3
FROM
(
SELECT
-- here if I write just "A.ID as ID" I will receive error of unfilled primary key column)
COALESCE( A.ID, B.ID, C.ID) as ID
, A.param_1 as param1
, B.param_2 as param2
, C.param_3 as param3
FROM
(
(SELECT ID, param_1 FROM single_param_table_1) A
FULL OUTER JOIN
(SELECT IِِD, param_2 FROM single_param_table_2) B on A.ID= B.ID
FULL OUTER JOIN
(SELECT ID, param_3 FROM single_param_table_3) C on A.ID = C.ID
-- or:
-- ISNULL(A.ID, B.ID)= C.ID
)
) as joined ;
The error message that I receive is as follows:
Violation of PRIMARY KEY constraint 'PK_joined_table'. Cannot insert duplicate key in object 'joined_table'.
It seems like parameter 3 is not completely separate from other parameters and in case it shares the key, repeated row is tried to be inserted into the table.
Ideally I wish to have the result joined_table as this:
ID | param 1 | param 2 | param 3
=======================================
123 | 11 | 12 | NULL
---------------------------------------
124 | NULL | NULL | 23