需要比较具有不同表结构的两个ORACLE表之间的数据并返回差异

时间:2016-01-04 19:10:38

标签: sql oracle compare

我试图比较两个具有不同表结构,不同列名的ORACLE表之间的数据。我需要一个ORACLE SQL查询来比较数据并返回不匹配的数据及其ID。两个表都具有相同的ID列,可用作比较链接。有谁可以指导我这个。

示例:表A(ID_A,QTY_A,DATE_A)
示例:表B(ID_B,QTY_B,DATE_B),其中ID_A = ID_B

3 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这应该足够了:

Select  *
From    TableA  A
Join    TableB  B   On  A.Id_A = B.Id_B
Where Not Exists
(
    Select  *
    From    TableB B2
    Where   B2.Id_B = B.Id_B
    And     (A.Qty_A = B.Qty_B Or Coalesce(A.Qty_A, B.Qty_B) Is Null)
    And     (A.Date_A = B.Date_B Or Coalesce(A.Date_A, B.Date_B) Is Null)
)

修改以包含NULL检查。

答案 1 :(得分:0)

您应该可以使用FULL OUTER JOIN

执行此操作
SELECT
    A.id,
    A.qty_a,
    B.qty_b,
    A.date_a,
    B.date_b
FROM
    Table_A A
FULL OUTER JOIN Table_B B ON B.id = A.id
WHERE
    (
        A.qty_a <> B.qty_b OR
        (A.qty_a IS NULL AND B.qty_b IS NOT NULL) OR
        (A.qty_a IS NOT NULL AND B.qty_b IS NULL)
    ) OR
    (
        A.date_a <> B.date_b
        (A.date_a IS NULL AND B.date_b IS NOT NULL) OR
        (A.date_a IS NOT NULL AND B.date_b IS NULL)
    )

答案 2 :(得分:0)

(
  (select id_a as id, qty_a as qty, date_a as date from table_a)
   minus
  (select id_b as id, qty_b as qty, date_b as date from table_b)
)
union
(
  (select id_b as id, qty_b as qty, date_b as date from table_b)
   minus
  (select id_a as id, qty_a as qty, date_a as date from table_a)
)