博客 拿几个案例讲讲translate|regexp_replace|listagg|wmsys.wm_concat|substr|regexp_substr常用函数-SQL开发实战系列(十一)

拿几个案例讲讲translate|regexp_replace|listagg|wmsys.wm_concat|substr|regexp_substr常用函数-SQL开发实战系列(十一)

   数栈君   发表于 2023-03-01 13:51  280  0

一、如何使用translate或regexp_replace提取姓名的大写首字母缩写
现在有一个需求:将下面临时表中的首大写字母,中间加".“显示为"Z.Y.D”:

with t as(
select 'Zhao Yan Dong' as aa from dual
)

我们可以利用regexp_replace的分组替换功能:

with t as(
select 'Zhao Yan Dong' as aa from dual
)
select regexp_replace(aa,'([[:upper:]])(.*)([[:upper:]])(.*)([[:upper:]])(.*)','\1.\3.\5') from t;
REGEXP_REPLACE(AA,'([[:UPPER:]])(.*)([[:UPPER:]])(.*)([[:UPPER:]])(.*)','\1.\3.\
--------------------------------------------------------------------------------
Z.Y.D

括号()将子表达式分组为一个替换单元、量词单元或后向引用单元。
在这个查询中,我们用()把符串分成了6个组,其中第1、3、5组中是大写字母,然后通过后向引用'\1.\3.\5'就分别取到了3个组的大写字母,并在中间增加了字符’.'。
也可以用前面介绍的translate函数。

SQL>
SQL> with t as(
2 select 'Zhao Yan Dong' as aa from dual
3 )
4 select translate(aa,1||' '||lower(aa),'1.') from t;

TRANSLATE(AA,1||''||LOWER(AA),'1.')
----------------------------------------------------------------
Z.Y.D

这个逻辑大家应该可以看得明白!

二、如何实现按字符串中的数字或字母排序
有下面一个临时表:

SQL>
SQL> with t as (
2 select dname||deptno||loc as aa
3 from dept
4 )
5 select * from t;

AA
-------------------------------------------------------------------
ACCOUNTING10NEW YORK
RESEARCH20DALLAS
SALES30CHICAGO
OPERATIONS40BOSTON

现在有个需求:想根据aa字段中的数值去排序!那么怎么做?
我们可以用正则表达式替换非数字字符,语句如下:

with t as
(select dname || deptno || loc as aa from dept)
select regexp_replace(aa, '[^[:digit:]]') as newaa, aa from t order by 1;

NEWAA AA
-------------------------------------------------------------------------------- -------------------------------------------------------------------
10 ACCOUNTING10NEW YORK
20 RESEARCH20DALLAS
30 SALES30CHICAGO
40 OPERATIONS40BOSTON

也可以用translate函数,直接替换掉非数字字符:

SQL> with t as
2 (select dname || deptno || loc as aa from dept)
3 select translate(aa,'0123456789'||aa,'0123456789') as newaa, aa from t order by 1;

NEWAA AA
-------------------------------------------------------------------------------- -------------------------------------------------------------------
10 ACCOUNTING10NEW YORK
20 RESEARCH20DALLAS
30 SALES30CHICAGO
40 OPERATIONS40BOSTON

三、如何聚合表中的行创建一个以逗号分隔拼接的字符串
现在有一个需求:将emp表相同部门的员工以逗号分隔合并到一起!!!
可能很多人已使用过wmsys.wm_concat函数,但wmsys.wm_concat是一个非公开函数,具有不确定性(返回值原来是varchar,Oracle11.2下就成了clob)。从Oracle11.2开始就有了分析函数listagg来替代它,到了Oracle 12C+及以后,wmsys.wm_concat函数已经被遗弃了,所以建议各位不要再使用这个函数了!至于listagg函数对于拼接超过4000长度的字符会报错问题,可以看博主这篇博客:
wm_concat 函数报错:ora06502-character string buffer to small

扯远了,接下来用listagg函数来实现上面的需求

SQL> select deptno,listagg(ename,',') within group(order by empno) as yonghu
2 from emp
3 group by deptno;

DEPTNO YONGHU
------ --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 SMITH,JONES,SCOTT,ADAMS,FORD
30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES
test

四、如何使用substr或regexp_substr提取第N个分隔符的子串
有如下临时表,每个部门的员工以逗号分隔:

SQL> select deptno,listagg(ename,',') within group(order by empno) as yonghu
2 from emp where deptno is not null
3 group by deptno;

DEPTNO YONGHU
------ --------------------------------------------------------------------------------
10 CLARK,KING,MILLER
20 SMITH,JONES,SCOTT,ADAMS,FORD
30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES

现在有一个需求:将上面临时表第二个子串取出来当作一列

没有正则表达式之前需要找到逗号的对应位置,然后对字符串进行截取:

with t as
(select deptno, listagg(ename, ',') within group(order by empno) as yonghu
from emp
where deptno is not null
group by deptno),
t1 as
(select deptno,
yonghu,
instr(yonghu, ',', 1, 1) as 第一个逗号位置,
instr(yonghu, ',', 1, 2) as 第二个逗号位置
from t)
select deptno,
yonghu,
case
when 第二个逗号位置 = 0 then
substr(yonghu, 第二个逗号位置 + 1)
else
substr(yonghu,
第一个逗号位置 + 1,
第二个逗号位置 - 第一个逗号位置 - 1)
end as newstr
from t1;

DEPTNO YONGHU NEWSTR
------ -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
10 CLARK,KING,MILLER KING
20 SMITH,JONES,SCOTT,ADAMS,FORD JONES
30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES WARD


而使用正则表达式就很简单了!!!看下面写法

with t as (
select deptno,listagg(ename,',') within group(order by empno) as yonghu
from emp where deptno is not null
group by deptno)
select deptno,yonghu,regexp_substr(yonghu,'[^,]+',1,2) as sub from t;

DEPTNO YONGHU SUB
------ -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
10 CLARK,KING,MILLER KING
20 SMITH,JONES,SCOTT,ADAMS,FORD JONES
30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES WARD

怎么样,有些时候用正则实现起来一些复杂的需求是不是特简单!!!这就是为啥博主钟爱正则的原因!

五、分解IP地址
现在有一个ip

with t as
(select '192.168.1.102' as ip from dual)

现在有个需求,要将这个ip以’.'拆分开来,那么使用传统的substr比较麻烦,但是正则的话就很简单,给出下面结果:

select regexp_count(ip, '\.'), regexp_substr(ip, '[^.]+', 1, level)
from t
connect by level <= regexp_count(ip, '\.');
REGEXP_COUNT(IP,'\.') REGEXP_SUBSTR(IP,'[^.]+',1,LEVEL)
--------------------- ----------------------------------------------------------------
3 192
3 168
3 1

当然,如果一个表里有多条数据,你这么写会发现问题,比如下面这个sql

with t as
(
select '192.168.1.102' as ip from dual
union all
select '10.1.1.103' as ip from dual
)
select regexp_count(ip, '\.'), regexp_substr(ip, '[^.]+', 1, level)
from t
connect by level <= regexp_count(ip, '\.');
REGEXP_COUNT(IP,'\.') REGEXP_SUBSTR(IP,'[^.]+',1,LEVEL)
--------------------- ---------------------------------
3 192
3 168
3 1
3 1
3 1
3 1
3 1
3 10
3 168
3 1
3 1
3 1
3 1
3 1

14 rows selected


总结
本篇文章相对上一篇数据处理文章有一定难度,但也是工作中常用的高级处理方式,我也算复习了一遍!


————————————————

免责申明:

本文系转载,版权归原作者所有,如若侵权请联系我们进行删除!

想了解或咨询更多有关袋鼠云大数据产品、行业解决方案、客户案例的朋友,浏览袋鼠云官网:https://www.dtstack.com/?src=bbs
同时,欢迎对大数据开源项目有兴趣的同学加入「袋鼠云开源框架钉钉技术群」,交流最新开源技术信息,群号码:30537511,项目地址:https://github.com/DTStack

0条评论
社区公告
  • 大数据领域最专业的产品&技术交流社区,专注于探讨与分享大数据领域有趣又火热的信息,专业又专注的数据人园地

最新活动更多
微信扫码获取数字化转型资料
钉钉扫码加入技术交流群