簡體   English   中英

甲骨文 SQL。 在同一表或不同表的兩個不同列和不同行中查找匹配值

[英]Oracle SQL. Find Matching values in two different columns and different rows from same table or different one

這是我在這個社區的第一個問題。 歡迎任何幫助。

想象一下我有一個這樣的表(它也可以在不同的表中有列,我不介意):

Account_Name_1:

Nike                
Pepsi              
Coke  

Account_Name_2:

Reebok
Coke
Nike  

我需要查詢“Account_Name_1”和“Account_Name_2”中的帳戶名稱列表

這將導致:

Accounts_in_both_columns

耐克

可樂

我怎樣才能做到這一點? 我試過 Inner Join,但我不確定,

謝謝 :)

額外的:

我也遇到了跨帳戶名稱命名不一致的問題,即使它們是同一個帳戶,其中一些名稱也不同。 例子:

Account_Name_1 Account_Name_2耐克銳步
百事可樂可樂耐克公司

如果我們像以前一樣運行相同的查詢,它只會列出“Coke”。

我已經閱讀了 UTL 匹配、Levenshtein 距離算法和 JARO_WINKLER_SIMILARITY 函數。 但是我無法創建具有相似性的值及其相似程度的列,因此我可以調查並確定它們是否是同一個帳戶。

請記住,這不是關於同一行匹配,而是兩列中的值匹配。 謝謝

我想你想要union

select account_name_1 as account_name
from t
union   -- on purpose to remove duplicates
select account_name_2
from t;

編輯:

如果您想要兩列中的值,只需使用exists

select distinct account_name_1 as account_name
from t
where exists (select 1
              from t t2
              where t2.account_name_2 = t.account_name_1
             );

據我了解這個問題,它是你正在尋找的intersect

SQL> with
  2  tab_1 (col) as
  3    (select 'Nike'   from dual union all
  4     select 'Pepsi'  from dual union all
  5     select 'Coke'   from dual
  6    ),
  7  tab_2 (col) as
  8    (select 'Reebok' from dual union all
  9     select 'Coke'   from dual union all
 10     select 'Nike'   from dual
 11    )
 12  -- code you need follows
 13  select col from tab_1
 14  intersect
 15  select col from tab_2;

COL
------
Coke
Nike

SQL>

暫無
暫無

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

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