Forum Discussion
Paata1966
Aug 09, 2023Copper Contributor
Can I merge two or more table by column and row.
Assuming I have a two table containing the following information: Table 1: Field1 | Field2
=====================
ABC | NULL
NULL | DEF Table 2: Field3 | Field4
=====================
ABC1 ...
olafhelper
Aug 10, 2023Bronze Contributor
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.
Paata1966
Aug 17, 2023Copper Contributor
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
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