2016-11-25 46 views
0

我需要比較兩個表中的數據,並檢查其歸因是不匹配的,表有相同的表定義,但問題是我力有一個獨特的密鑰進行比較。我試圖用比較不會有兩個表唯一鍵

CONCAT(CONCAT(CONCAT(table1.A, Table1.B)) 
=CONCAT(CONCAT(CONCAT(table2.A, Table2.B)) 

,但仍面臨着重複行也試圖NVL上幾列,但沒有奏效

SELECT 
    UT.cat, 
    PD.cat 
FROM 
    EM UT, EM_63 PD 
WHERE 
    NVL(UT.cat, 1) = NVL(PD.cat, 1) AND 
    NVL(UT.AT_NUMBER, 1) = NVL(PD.AT_NUMBER, 1) AND 
    NVL(UT.OFFSET, 1) = NVL(PD.OFFSET, 1) AND 
    NVL(UT.PROD, 1) = NVL(PD.PROD, 1) 
; 

有34K的記錄在另一個表中的一個表35K的記錄,但如果我運行上述查詢,行數爲3百萬。

列在表:

COUNTRY  
CATEGORY 
TYPE  
DESCRIPTION 

樣本數據:

表1:

COUNTRY CATEGORY TYPE DESCRIPTION  
US   C  T1  In 
IN   A  T2  OUT 
B   C  T2  IN 
Y   C  T1  INOUT 

表2:

COUNTRY CATEGORY TYPE DESCRIPTION  
US   C  T2  In 
IN   B  T2  Out 
Q   C  T2  IN 

預期輸出:

column  Matched unmatched 
COUNTRY   2  1 
CATEGORY  2  1 
TYPE   2  1 
DESCRIPTION  3  0 
+0

添加一些樣本表數據和預期的結果。 (以及格式化文本。) – jarlh

+0

也許'除了'將有助於 – HoneyBadger

+2

爲什麼三聯? –

回答

2

在最一般的情況下(當你可能有重複的行,和你想要查看哪個行存在於一個表中但不存在於另一個表中,並且也沒有存在於兩個表中的行,但該行在第一個表中存在3次,但在另一箇中存在5次):

這是由於某種原因,它似乎是大多數人的解決「最佳解決方案」的常見問題仍然沒有意識到,即使它是在很多年前在AskTom上開發出來的,並且已經提交了很多次。

你不需要加入,你不需要任何形式的獨特的鍵,你不需要看任何一個表中不止一次。想法是添加兩列以顯示每行來自哪個表,執行UNION ALL,然後GROUP BY除「源」列之外的所有列,並顯示每個表的計數。事情是這樣的:

select count(t_1) as count_table_1, count(t_2) as count_table_2, col1, col2, ... 
from  (
      select 'x' as t_1, null as t_2, col1, col2, ... 
      from table_1 
      union all 
      select null as t_1, 'x' as t_2, col1, col2, ... 
      from table_2 
     ) 
group by col1, col2, ... 
having count(t_1) != count(t_2) 
; 
+0

感謝對策的探討,但我們可以得到輸出中像列名,匹配計數,unmatchedcount –

+0

@varoo - 什麼叫「匹配計數」是什麼意思?你想按列來做這個?我不確定這是否合理。請張貼一小部分結果應該是什麼樣的樣本(不要擔心它是如何實現的)以及它的意思。 – mathguy

+0

更新了示例數據 –

0

CONCAT的問題是,你可以得到無效的比賽中,如果你的數據看起來與此類似:

table1.A = '123' 
table1.B = '456' 

會連接到:'123456'

table2.A = '12' 
table2.B = '3456' 

會連接也:'123456'

您必須單獨比較這些字段:table1.A = table2.A AND table1.B = table2.B

+0

我有試過比較table1.A = table2.A,但是我也需要計數(*)不同的數據。 –

1

開始與此查詢,以檢查是否這些4列形成的關鍵。

select  occ_total,occ_ut,occ_pd 
      ,count(*)    as records 

from  (select  count (*)        as occ_total 
         ,count (case tab when 'UT' then 1 end) as occ_ut 
         ,count (case tab when 'PD' then 1 end) as occ_pd 

      from     select 'UT' as tab,cat,AT_NUMBER,OFFSET,PROD from EM 
         union all select 'PD'  ,cat,AT_NUMBER,OFFSET,PROD from EM_63 PD 
         ) t 

      group by cat,AT_NUMBER,OFFSET,PROD 
      ) t 

group by occ_total,occ_ut,occ_pd  

order by records desc 
; 

你選擇了你的「鑰匙」後,您可以使用下面的查詢看到屬性的值

select  count (*)        as occ_total 
      ,count (case tab when 'UT' then 1 end) as occ_ut 
      ,count (case tab when 'PD' then 1 end) as occ_pd 

      ,count (distinct att1)     as cnt_dst_att1 
      ,count (distinct att2)     as cnt_dst_att2 
      ,count (distinct att3)     as cnt_dst_att3 
      ,... 
      ,listagg (case tab when 'UT' then att1 end) within group (order by att1) as att1_vals_ut 
      ,listagg (case tab when 'PD' then att1 end) within group (order by att1) as att1_vals_pd 
      ,listagg (case tab when 'UT' then att2 end) within group (order by att2) as att2_vals_ut 
      ,listagg (case tab when 'PD' then att2 end) within group (order by att2) as att2_vals_pd 
      ,listagg (case tab when 'UT' then att3 end) within group (order by att3) as att3_vals_ut 
      ,listagg (case tab when 'PD' then att3 end) within group (order by att3) as att3_vals_pd 
      ,... 

from     select 'UT' as tab,cat,AT_NUMBER,OFFSET,PROD,att1,att2,att3,... from E M 
      union all select 'PD'  ,cat,AT_NUMBER,OFFSET,PROD,att1,att2,att3,... from EM_63 PD 
      ) t 

group by cat,AT_NUMBER,OFFSET,PROD 
;