如何在Oracle中识别重复项以及特殊字符

时间:2019-03-21 06:47:13

标签: string oracle

我有Employer表,其中有两列,Ecode和Ename。

oDoc1

我需要输出包含ecode empname E1 NIKHIL E2 Nikhil. E3 Nikhil?? E4 sunita.. E5 sunita.. E6 sunita E7 Mangesh E8 HIREN E9 HIREN.. E10 HIREN?? E11 Viren 的重复值,而忽略特殊字符。 EmpnameNikhilNikhil.都应该成为我的输出的一部分。

预期输出:-

Nikhil?

2 个答案:

答案 0 :(得分:2)

with test (ecode, empname) as
(
select 'E1'  ,'NIKHIL'    from dual union all
select 'E2'  ,'Nikhil.'   from dual union all
select 'E3'  ,'Nikhil??'  from dual union all
select 'E4'  ,'sunita..'  from dual union all
select 'E5'  ,'sunita..'  from dual union all
select 'E6'  ,'sunita'    from dual union all
select 'E7'  ,'Mangesh'   from dual union all
select 'E8'  ,'HIREN'     from dual union all
select 'E9'  ,'HIREN.'    from dual union all
select 'E10' ,'HIREN??'   from dual union all
select 'E11' ,'Viren'     from dual
)
select ecode, empname,
dense_rank() over (order by upper(regexp_replace(empname, '\W'))) grp
from
   (select t.*,
    count(*) over (partition by upper(regexp_replace(empname, '\W'))) cnt
    from test t
   )
where cnt > 1
order by upper(regexp_replace(empname, '\W')), empname;

ECO EMPNAME         GRP
--- -------- ----------
E8  HIREN             1
E9  HIREN.            1
E10 HIREN??           1
E1  NIKHIL            2
E2  Nikhil.           2
E3  Nikhil??          2
E6  sunita            3
E5  sunita..          3
E4  sunita..          3

9 rows selected.

答案 1 :(得分:0)

这是一个选择:通过除去字母(EMPNAME就是这样)来查找'[^[:alpha:]]'列上的重复项:

SQL> with test (ecode, empname) as
  2  (
  3  select 'E1'  ,'NIKHIL'    from dual union all
  4  select 'E2'  ,'Nikhil.'   from dual union all
  5  select 'E3'  ,'Nikhil??'  from dual union all
  6  select 'E4'  ,'sunita..'  from dual union all
  7  select 'E5'  ,'sunita..'  from dual union all
  8  select 'E6'  ,'sunita'    from dual union all
  9  select 'E7'  ,'Mangesh'   from dual union all
 10  select 'E8'  ,'HIREN'     from dual union all
 11  select 'E9'  ,'HIREN.'    from dual union all
 12  select 'E10' ,'HIREN??'   from dual union all
 13  select 'E11' ,'Viren'     from dual
 14  ),
 15  inter as
 16    (select ecode, empname, upper(regexp_replace(empname, '[^[:alpha:]]', '')) rc
 17     from test
 18    )
 19  select ecode, empname
 20  from inter
 21  where rc in (select rc from inter
 22               group by rc
 23               having count(*) > 1
 24              )
 25  order by to_number(regexp_substr(ecode, '\d+$'));

ECO EMPNAME
--- --------
E1  NIKHIL
E2  Nikhil.
E3  Nikhil??
E4  sunita..
E5  sunita..
E6  sunita
E8  HIREN
E9  HIREN.
E10 HIREN??

9 rows selected.

SQL>