nay*_*arb 1 oracle join oracle8
对于每个表,我有两个具有相同列,Item Code和Qty的表:
TABLE A TABLE B
-------------- -------------
X 2 X 1
Y 1 S 2
Z 5 Z 5
Run Code Online (Sandbox Code Playgroud)
我想要获得的结果是这样的:
Table C
---------------
X 2 1
Y 1 0
S 0 2
Run Code Online (Sandbox Code Playgroud)
我只需要qty两个表中不同的项 (包括应显示为零的空值).
注意:我使用的是Oracle8,因此无法使用ANSI FULL OUTER JOIN.
Tar*_*ryn 12
编辑,由于该问题特定于不使用ANSI语法的Oracle 8,因此以下内容应该有效:
select col1,
nvl(a_col2, 0) as a_col2,
nvl(b_col2, 0) as b_col2
from
(
select a.col1, a.col2 as a_col2, b.col2 as b_col2
from TableA a, TableB b
where a.col1 = b.col1(+)
union
select b.col1, a.col2 as a_col2, b.col2 as b_col2
from TableA a, TableB b
where a.col1(+) = b.col1
)
where a_col2 <> b_col2
or (a_col2 is null or b_col2 is null)
Run Code Online (Sandbox Code Playgroud)
请参阅SQL Fiddle with Demo.这将返回:
| COL1 | A_COL2 | B_COL2 |
--------------------------
| S | 0 | 2 |
| X | 2 | 1 |
| Y | 1 | 0 |
Run Code Online (Sandbox Code Playgroud)
如果您使用的是支持ANSI语法的Oracle版本,则可以使用以下命令FULL OUTER JOIN:
select
coalesce(a.col1, b.col1) col1,
coalesce(a.col2, 0) a_col2,
coalesce(b.col2, 0) b_col2
from tablea a
full outer join tableb b
on a.col1 = b.col1
where a.col2 <> b.col2
or (a.col2 is null or b.col2 is null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8549 次 |
| 最近记录: |