暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 用例语句比较两列

askTom 2017-04-21
214

问题描述

嗨,汤姆,

您能否建议在比较两个表之间的字段时使用case语句的最佳方法。

创建表t1 (id号,first_name varchar2(20),last_name varchar2(20),birth_date date);

插入t1 (id,first_name,last_name,birth_date) 值 (1,'John','Walker','19500101 ');
插入t1 (id,first_name,last_name,birth_date) 值 (2,'Haley','grant-Boon','19510101 ');
插入t1 (id,first_name,last_name,birth_date) 值 (3,'Nikki','Alaska Santa','19530101 ');
插入t1 (id,first_name,last_name,birth_date) 值 (4,'Jimmy','Cole Haan','19540101 ');
插入t1 (id,first_name,last_name,birth_date) 值 (5,'Neil','O''Brian','19550101 '); ----- last_name是O'Brian。我使用了两个引号来启用成功的插入。

创建表t2 (id号,first_name varchar2(20),last_name varchar2(20),birth_date date);

插入到t2 (id,first_name,last_name,birth_date) 值 (1,'John','Walker','19500101 ');
插入到t2 (id,first_name,last_name,birth_date) 值 (2,'Haley','Grant Boon','19510101 ');
插入到t2 (id,first_name,last_name,birth_date) 值 (3,'Nikki','Alaska Santa','19530101 ');
插入到t2 (id,first_name,last_name,birth_date) 值 (4,'Jimmy','cole-Haan','19540101 ');
插入t1 (id,first_name,last_name,birth_date) 值 (4,'Neil','OBrian','19550101 ');


选择t1.id,t1.first _ 名称,t1.last _ 名称
,t2.id,t2.first _ 名称,t2.last _ 名称
从t1、t2开始
其中t1.birth _ date = t2.birth _ date
t1.id = t2.id
----
---- 名字检查逻辑
----
和 (案例
当REGEXP_INSTR (SUBSTR (t1.first _ name,1,10),'[([: 空白:]-,* _ '') ]')> 0时,则
SUBSTR (t1.first _ name,1,10),1,REGEXP_INSTR (SUBSTR (t1.first _ name,1,10),'[([: 空白:]-,* _ '') ]') - 1)
其他
SUBSTR (t1.first _ 名称,1,10)
结束) = (案例
当REGEXP_INSTR (substr(t2.First _ Name,1,10),'[([: 空白:]-,* _ '') ]')> 0时,则
SUBSTR (substr(t2.First _ Name,1,10),1,REGEXP_INSTR (substr(t2.First _ Name,1,10),'[([: 空白:]-,* _ '') ]')- 1)
其他
substr(t2.First _ 名称,1,10)
结束)
-----------------------------------
---- LastName检查逻辑
-----------------------------------
和 (案例
当REGEXP_INSTR (SUBSTR (t1.last _ name,1,14),'[([: 空白:]-,* _) ]')> 0时,则
SUBSTR (t1.last _ 名称,1,14),1,REGEXP_INSTR (SUBSTR (t1.last _ 名称,1,14),'[([: 空白:]-,* _) ]')- 1)
其他
SUBSTR (t1.last _ 名称,1,14)
结束) = (案例
当REGEXP_INSTR (substr(t2.Last _ Name,1,14),'[([: 空白:]-,* _) ]')> 0时,则
SUBSTR (substr(t2.Last _ Name,1,14),1,REGEXP_INSTR (substr(t2.Last _ Name,1,14),'[([: 空白:]-,* _) ]') - 1)
其他
substr(t2.Last _ 名称,1,14)
结束)
)

您能否帮助我进行上述查询。使用case语句比较值是否有效。

我们想比较一下名字,即OBrian = O'Brian。和Cole-Haan = Cole Haan。在这些情况下,记录应返回true。

谢谢

专家解答

您可以将UTL_MATCH函数与仅保留alpha字符的regexp结合使用,例如


SQL> create table t1(id number, first_name varchar2(20), last_name varchar2(20), birth_date varchar2(20));

Table created.

SQL>
SQL> insert into t1 (id, first_name, last_name, birth_date) values (1, 'John', 'Walker', '19500101');

1 row created.

SQL> insert into t1 (id, first_name, last_name, birth_date) values (2, 'Haley', 'Grant-Boon', '19510101');

1 row created.

SQL> insert into t1 (id, first_name, last_name, birth_date) values (3, 'Nikki', 'Alaska Santa', '19530101');

1 row created.

SQL> insert into t1 (id, first_name, last_name, birth_date) values (4, 'Jimmy', 'Cole Haan', '19540101');

1 row created.

SQL> insert into t1 (id, first_name, last_name, birth_date) values (5, 'Neil', 'O''Brian', '19550101');

1 row created.

SQL>
SQL> create table t2(id number, first_name varchar2(20), last_name varchar2(20), birth_date varchar2(20));

Table created.

SQL> insert into t2 (id, first_name, last_name, birth_date) values (1, 'John', 'Walker', '19500101');

1 row created.

SQL> insert into t2 (id, first_name, last_name, birth_date) values (2, 'Haley', 'Grant Boon', '19510101');

1 row created.

SQL> insert into t2 (id, first_name, last_name, birth_date) values (3, 'Nikki', 'Alaska Santa', '19530101');

1 row created.

SQL> insert into t2 (id, first_name, last_name, birth_date) values (4, 'Jimmy', 'Cole-Haan', '19540101');

1 row created.

SQL> insert into t2 (id, first_name, last_name, birth_date) values (5, 'Neil', 'OBrian', '19550101');

1 row created.

SQL>
SQL> with t1x as
  2    ( select id,
  3             lower(regexp_replace(last_name,'[^[:alpha:]]*')) last_name,
  4             lower(regexp_replace(first_name,'[^[:alpha:]]*')) first_name,
  5             birth_date
  6      from   t1 ),
  7   t2x as
  8    ( select id,
  9             lower(regexp_replace(last_name,'[^[:alpha:]]*')) last_name,
 10             lower(regexp_replace(first_name,'[^[:alpha:]]*')) first_name,
 11             birth_date
 12      from   t2 )
 13  select
 14    t1.id,
 15    case
 16      when t1.last_name = t2.last_name and t1.first_name = t2.first_name then
 17            'BOTH CHAR MATCH'
 18      when  ( t1.last_name = t2.last_name and utl_match.edit_distance(t1.first_name,t2.first_name) <= 2  ) or
 19            ( t1.first_name = t2.first_name and utl_match.edit_distance(t1.last_name,t2.last_name) <= 2  ) then
 20            'ONE NAME MATCH, CLOSE ON OTHER'
 21      when  t1.last_name = t2.last_name or t1.first_name = t2.first_name then
 22            'ONE NAME MATCH'
 23    end matching
 24  from t1x t1, t2x t2
 25  where t1.birth_date = t2.birth_date
 26  and t1.id = t2.id;

        ID MATCHING
---------- ------------------------------
         1 BOTH CHAR MATCH
         2 BOTH CHAR MATCH
         3 BOTH CHAR MATCH
         4 BOTH CHAR MATCH
         5 BOTH CHAR MATCH

5 rows selected.

SQL>


「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论