oracle wm_concat 替换函数,wm_concat 函数在PG中替代
WM_CONCAT 经常使用到行转列上,早期的代码里这个函数用的会比较多,但是可惜在12c中,这个函数已经过期了:
所以,在后续的开发中,不要再使用这个函数。在MOS中,Oracle也不建议客户使用这个函数,该函数为系统内部使用:
那PG中有没有类似的函数呢?答案是肯定的:string_agg.
创建测试数据
create table t_concat (id int,name varchar(100),score int); # pg
create table t_concat (id number,name varchar2(100),score number); # Oracle
truncate table t_concat;
insert into t_concat values (1,'yuwen',90);
insert into t_concat values (1,'shuxue',85);
insert into t_concat values (1,'yingyu',70);
insert into t_concat values (1,'wuli',80);
insert into t_concat values (1,'huaxue',74);
insert into t_concat values (2,'yuwen',91);
insert into t_concat values (2,'shuxue',90);
insert into t_concat values (2,'yingyu',73);
insert into t_concat values (2,'wuli',78);
insert into t_concat values (2,'huaxue',74);
查询
# Oracle
select id,sum(score),wm_concat(name) from t_concat group by id order by id;
SQL> select id,sum(score),wm_concat(name) from t_concat group by id order by id;
ID SUM(SCORE) WM_CONCAT(NAME)
---------- ---------- --------------------------------------------------------------------------------
1 399 yuwen,huaxue,wuli,yingyu,shuxue
2 406 yuwen,huaxue,wuli,yingyu,shuxue
# pg
select id,sum(score),string_agg(name,',') from t_concat group by id order by id;
test=# select id,sum(score),string_agg(name,',') from t_concat group by id order by id;
id | sum | string_agg
----+-----+---------------------------------
1 | 399 | yuwen,shuxue,yingyu,wuli,huaxue
2 | 406 | yuwen,shuxue,yingyu,wuli,huaxue
(2 rows)
pg可以指定其中的分隔符:
test=# select id,sum(score),string_agg(name,'-') from t_concat group by id order by id;
id | sum | string_agg
----+-----+---------------------------------
1 | 399 | yuwen-shuxue-yingyu-wuli-huaxue
2 | 406 | yuwen-shuxue-yingyu-wuli-huaxue
(2 rows)
test=# select id,sum(score),string_agg(name,'||') from t_concat group by id order by id;
id | sum | string_agg
----+-----+-------------------------------------
1 | 399 | yuwen||shuxue||yingyu||wuli||huaxue
2 | 406 | yuwen||shuxue||yingyu||wuli||huaxue
(2 rows)
test=#
在Oracle官方文档中,从11gr2开始,建议使用listagg代替wm_concat:
col names format a60;
select id,sum(score),listagg(name,'-') within group (order by score ) as "names"from t_concat group by id order by id;
SQL> SQL> select id,sum(score),listagg(name,'-') within group (order by score ) as "names"from t_concat group by id order by id;
ID SUM(SCORE) names
---------- ---------- ------------------------------------------------------------
1 399 yingyu-huaxue-wuli-shuxue-yuwen
2 406 yingyu-huaxue-wuli-shuxue-yuwen
SQL>
select id,sum(score),listagg(name) within group (order by name ) as "names"from t_concat group by id order by id;
ID SUM(SCORE) names
---------- ---------- ------------------------------------------------------------
1 399 huaxueshuxuewuliyingyuyuwen
2 406 huaxueshuxuewuliyingyuyuwen
SQL>
分隔符不是必选参数。PG的string_agg的分隔符为必选参数:
test=# select id,sum(score),string_agg(name) from t_concat group by id order by id;
ERROR: function string_agg(character varying) does not exist
LINE 1: select id,sum(score),string_agg(name) from t_concat group by...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
test=#
参考链接:
oracle wm_concat 替换函数,wm_concat 函数在PG中替代相关推荐
- oracle wm_concat 替换函数,Oracle 10g无法使用listagg函数的替代解决方案[wm_concat]
LISTAGG函数介绍 LISTAGG函数是Oracle 11.2新增的函数,用于字符串拼接,11.2之前的版本无法使用,先来简单介绍一下listagg函数的使用 LISTAGG完整语法 以上是官方文 ...
- Oracle内部函数 wmsys.wm_concat 替换办法及思考
如果你不知道这个函数没有关系,因为您可以有其他的办法来实现.如果你已经在使用这个函数,一定要注意. wmsys.wm_concat 是ORACLE内部函数,没有对外公布,也就是说,你可以使用,但是如果 ...
- PostgreSQL Oracle 兼容性之 - PL/SQL DETERMINISTIC 与PG函数稳定性(immutable, stable, volatile)...
标签 PostgreSQL , Oracle , 函数稳定性 , stable , immutable , volatile , DETERMINISTIC 背景 Oracle创建pl/sql函数时, ...
- 程序包或函数 WM_CONCAT 处于无效状态
今天运行了很久的项目,点了一下查询突然发现出现了一个 程序包或函数 WM_CONCAT 处于无效状态的问题报错,妈耶,因为啥啊,咋地了,跑了这么就怎么就瘫痪了 废话不多说,找到报错的sql 然后勒,当 ...
- oracle汉字替换星号,更加实用的识别汉字(GBK)的自定义函数
本帖最后由 atgc 于 2014-1-5 19:19 编辑 几年前写过一个识别汉字的函数 http://www.itpub.net/thread-847680-1-1.html 但是只能识别GB23 ...
- Oracle中的within,Oracle函数 --聚合函数中的语法within group
Oracle的聚合函数一般与group by 联合使用,但一般通过group by 聚合 但某些聚合函数会后跟 WITHIN GROUP (ORDER BY expr [ DESC | ASC ] [ ...
- oracle within的用法,Oracle的 listagg() WITHIN GROUP ()函数使用
1.使用条件查询 查询部门为20的员工列表 -- 查询部门为20的员工列表 SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO = '20 ...
- java oracle 流水号_Oracle生成流水号函数
一.参考 1:日期范围上 smalldatetime的有效时间范围1900/1/1~2079/6/6 datetime的有效时间范围1753/1/1~9999/12/31 2:精准度上 smallda ...
- Oracle之常用内置函数
1.Oracle内置函数--wm_concat() wm_concat()函数是oracle中独有的,mysql中有一个group_concat()函数. 实现行转列功能,即将查询出的某一列值使用逗号 ...
最新文章
- latex中插入代码
- MySQL查询的进阶操作--连接查询
- 不同制式字符串之间的转换
- harmonyos2.0如何申请,华为鸿蒙HarmonyOS2.0手机开发者Beta版公测申请地址方法_专题_53货源网...
- Runtime类及其常用方法
- 初识Entity Framework CodeFirst(2)
- java.time.format例子_java格式化时间示例
- Linux的 .bashrc 和.bash_profile和.profile文件
- js for in 获得遍历数组索引和对象属性
- 简易RAM的C++实现
- Visual Studio 2019 配置汇编和masm32教程
- 富文本编辑器Ueditor实战(一)
- 半导体器件制造封装材料和生产工艺流程(图文介绍)
- oracle inst 自动重启,oracle rac 节点自动重启
- 006网易-表达式求值
- html网页屏保,屏幕保护是什么
- iPad安卓协议是怎么实现功能的
- Docker安装chemexIT资产管理系统
- pmp中ram和raci的区别_PMP学习之路
- 浏览器主页被hao.qq劫持