Hi,
I have to join data from 2 tables, say A and B, so that data from A takes priority over B. e.g. A has data
CD NAME
1 a
2 b
and B has
CD NAME
2 bb
3 cc
The result should be:
1 a
2 b
3 cc
I am doing it as follows:
SELECT DECODE(A.CD,NULL,B.CD,A.CD) CD, DECODE(A.CD,NULL,A.NAME,B.NAME) NAME
FROM A FULL OUTER JOIN B ON A.CD = B.CD
and it works fine. But if I have say 100 columns in each table, I will be doing 100 decodes in the statement. I am not sure if this is the best way to do it.
The other way I can think of is as follows:
SELECT A.CD, A.NAME FROM A
UNION ALL
(SELECT B.CD, B.NAME FROM B MINUS SELECT A.CD, A.NAME FROM A)
But this way I will be accessing table A twice.
Can you please suggest what's the best way to do it? I am using Oracle.
Thanks.
I have to join data from 2 tables, say A and B, so that data from A takes priority over B. e.g. A has data
CD NAME
1 a
2 b
and B has
CD NAME
2 bb
3 cc
The result should be:
1 a
2 b
3 cc
I am doing it as follows:
SELECT DECODE(A.CD,NULL,B.CD,A.CD) CD, DECODE(A.CD,NULL,A.NAME,B.NAME) NAME
FROM A FULL OUTER JOIN B ON A.CD = B.CD
and it works fine. But if I have say 100 columns in each table, I will be doing 100 decodes in the statement. I am not sure if this is the best way to do it.
The other way I can think of is as follows:
SELECT A.CD, A.NAME FROM A
UNION ALL
(SELECT B.CD, B.NAME FROM B MINUS SELECT A.CD, A.NAME FROM A)
But this way I will be accessing table A twice.
Can you please suggest what's the best way to do it? I am using Oracle.
Thanks.