簡體   English   中英

Oracle SQL比較數據並檢索Oracle DB中兩個表之間的不匹配行

[英]Oracle SQL to compare data and retrieve un-matching rows between two tables in Oracle DB

我正在嘗試使用Oracle SQL比較兩個表之間的數據。 SQL必須比較數據,並且必須從表中返回不匹配的數據。 有人有執行此操作的想法嗎? 我可能會使用SQL或使用Informatica來執行相同的操作。 請幫幫我。 提前致謝。

注意:它們可能具有相同的結構,因為我將創建ID,數量,價格的TEMP表A。 A將與B(ID,數量,價格)進行比較。 A來自EBS,TEMP A在Oracle中創建。 B來自數據倉庫,並且存在於Oracle DB中。

您可以使用集合比較2個表中的行:取兩個表的並集並從中減去它們的交集。

(
select id, qty, price from table_a
union
select id, qty, price from table_b
)
minus
(
select id, qty, price from table_a
intersect
select id, qty, price from table_b
);

這將為您提供哪些行與相反的表不匹配,但是您將無法從該查詢中分辨出哪個行來自哪個表。 將結果與原始表連接起來應該很容易做到(或者再次相交,盡管在表具有主鍵的情況下連接應該更便宜)。

with t as (
(
select id, qty, price from table_a
union
select id, qty, price from table_b
)
minus
(
select id, qty, price from table_a
intersect
select id, qty, price from table_b
))
select *
  from t
  join table_a a on t.id = a.id;

加入table_b時同樣適用,以找出哪些行與table_a不匹配。

運行樣本:

SQL> create table table_a (id number, qty number, price number);
Table created
SQL> insert into table_a values (1, 100, 1.1);
1 row inserted
SQL> insert into table_a values (2, 200, 2.2);
1 row inserted
SQL> insert into table_a values (3, 300, 3.3);
1 row inserted
SQL> create table table_b (id number, qty number, price number);
Table created
SQL> insert into table_b values (1, 100, 1.1);
1 row inserted
SQL> insert into table_b values (2, 200, 2.2);
1 row inserted
SQL> insert into table_b values (4, 300, 3.3);
1 row inserted
SQL> insert into table_b values (5, 500, 5.5);
1 row inserted
SQL> (
  2  select id, qty, price from table_a
  3  union
  4  select id, qty, price from table_b
  5  )
  6  minus
  7  (
  8  select id, qty, price from table_a
  9  intersect
 10  select id, qty, price from table_b
 11  );
        ID        QTY      PRICE
---------- ---------- ----------
         3        300        3,3
         4        300        3,3
         5        500        5,5
SQL> with t as (
  2  (
  3  select id, qty, price from table_a
  4  union
  5  select id, qty, price from table_b
  6  )
  7  minus
  8  (
  9  select id, qty, price from table_a
 10  intersect
 11  select id, qty, price from table_b
 12  ))
 13  select *
 14    from t
 15    join table_a a on t.id = a.id;
        ID        QTY      PRICE         ID        QTY      PRICE
---------- ---------- ---------- ---------- ---------- ----------
         3        300        3,3          3        300        3,3

SQL> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM