Can I merge two or more table by column and row.

Copper Contributor

Assuming I have a two table containing the following information:

Table 1:

Field1 | Field2
=====================
ABC    | NULL
NULL    | DEF

Table 2:

Field3 | Field4
=====================
ABC1    | ABC2
NULL    | DEF2
DEF4 | DEF3

 

is there a way I can perform a selection on the tables to get the following Table:

Field1 | Field2 | Field3 | Field4
==========================
ABC    | NULL    | ABC1   | ABC2
NULL   | DEF    NULL | DEF2 
NULL     | NULL | DEF4 | DEF3

Thanks

3 Replies

@Paata1966 , the requirements on the result are unlogical and unlogical is difficult to implement in SQL

Why is table1, field2 = "Def" only once included in the result? Unlogical.

@olafhelper Thanks for the answer, I think it's logical enough, I solved the problem using ROW_NUMBER and LEFT JOIN

SELECT t1.Field1,
t1.Field2,
t2.Field3,
t2.Field4
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY Field1) as rn, *
FROM Table1
) t1
LEFT JOIN
(
SELECT ROW_NUMBER() OVER (ORDER BY Field3) as rn, *
FROM Table2

) t2
ON t1.rn = t2.rn