簡體   English   中英

Oracle:比較兩個不同表中沒有主鍵的字符串列以查找匹配/不匹配的字符串

[英]Oracle: Comparing string columns from two different table without a primary key to find match/unmatched string

我有兩個表,我們想比較這些表中的兩個字符串列。 這些表中沒有與 where 子句匹配的主鍵。 表格如下: 比較應該忽略:

  1. 忽略字符串中任何位置的空格 2) 忽略區分大小寫

在此處輸入圖像描述

OUTPUT 想要:

在此處輸入圖像描述

您可以使用full outer joinupper連接,並在連接條件中replace function,如下所示:

Select t1.str, t2.str,
       Case when t1.str is not null and t2.str is not null then 'exist in both table'
             when t1.str is not null then 'missing in table2'
             Else 'missing in table1'
       End as differences
From table1 t1 full join table2 t2
  On upper(replace(t1.str,' ','') = upper(replace(t2.str,' ','')

嘗試以下 SQL 一次,看看是否有效。 否則,您能否分享那些它說“表 2 中缺失”的值,即使它在兩個表中都存在?

Select t1.str, t2.str,
       Case when nvl(t1.str,'X') != 'X' and nvl(t2.str,'X') != 'X' then 'exist in both table'
             when nvl(t1.str,'X') != 'X' then 'missing in table2'
             Else 'missing in table1'
       End as differences
From table1 t1 full join table2 t2
  On upper(trim(t1.str)) = upper(trim(t2.str))

相似地; 第 1 - 16 行的樣本數據。您可能需要的查詢從第 17 行開始。

SQL> with
  2  tab1 (col) as
  3    (select 'mg/kl'     from dual union all
  4     select 'k/mg'      from dual union all
  5     select 'l/g/kg'    from dual union all
  6     select 'umol_Ulp'  from dual union all
  7     select '10*12_/KG' from dual union all
  8     select 'a/L/G'     from dual
  9    ),
 10  tab2 (col) as
 11    (select '10*12_/kg' from dual union all
 12     select '1/L/G'     from dual union all
 13     select 'PG/7@R'    from dual union all
 14     select 'KG/CM/L'   from dual union all
 15     select 'l/g/kg'    from dual
 16    )
 17  select a.col, b.col,
 18    case when lower(a.col) = lower(b.col) then
 19              'exists in both tables ' ||
 20              case when a.col = b.col then 'and exact match'
 21                   else 'but different'
 22              end
 23         when a.col is null then 'missing in table 1'
 24         when b.col is null then 'missing in table 2'
 25    end differences
 26  from tab1 a full outer join tab2 b on lower(a.col) = lower(b.col);

COL       COL       DIFFERENCES
--------- --------- -------------------------------------
mg/kl               missing in table 2
k/mg                missing in table 2
l/g/kg    l/g/kg    exists in both tables and exact match
umol_Ulp            missing in table 2
10*12_/KG 10*12_/kg exists in both tables but different
a/L/G               missing in table 2
          KG/CM/L   missing in table 1
          PG/7@R    missing in table 1
          1/L/G     missing in table 1

9 rows selected.

SQL>

暫無
暫無

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

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