Oracle 11g 列转行listagg

在Oracle 11g之前列转行有些麻烦,11g之后,非常简单。现在有功能的业务是,有一张test的表记录的是单据的审批信息,id为审批信息的主键,sheet_id为外键,是单据的id,remark为审批的内容,在前端的列表页面上,要看到这个单据所有的审批信息,要显示在一个格子里面。

SQL> drop table test purge;

SQL> create table test

(

id  number(10),

sheet_id number(10),

remark varchar2(50)

);

SQL> insert into test values(1,100,‘审批意见1‘);

SQL> insert into test values(2,100,‘审批意见2‘);

SQL> insert into test values(3,100,‘审批意见3‘);

SQL> insert into test values(4,200,‘同意1‘);

SQL> insert into test values(5,200,‘同意2‘);

SQL> insert into test values(6,200,‘同意3‘);

SQL> insert into test values(7,300,‘回退1‘);

SQL> insert into test values(8,300,‘回退2‘);

SQL> commit;

SQL> col C_REMARK format a40;

SQL> select sheet_id,listagg(remark,‘,‘) within GROUP (order by id) as c_remark

from test

group by sheet_id;

SHEET_ID C_REMARK

---------- ----------------------------------------

100 审批意见1,审批意见2,审批意见3

200 同意1,同意2,同意3

300 回退1,回退2

还有一种写法:

SQL> select sheet_id, listagg(remark, ‘,‘) within

GROUP(

order by id) over(partition by sheet_id) c_remark

from test;

SHEET_ID C_REMARK

---------- ----------------------------------------

100 审批意见1,审批意见2,审批意见3

100 审批意见1,审批意见2,审批意见3

100 审批意见1,审批意见2,审批意见3

200 同意1,同意2,同意3

200 同意1,同意2,同意3

200 同意1,同意2,同意3

300 回退1,回退2

300 回退1,回退2

SQL> select distinct sheet_id, listagg(remark, ‘,‘) within

GROUP(

order by id) over(partition by sheet_id) c_remark

from test;

SHEET_ID C_REMARK

---------- ----------------------------------------

200 同意1,同意2,同意3

100 审批意见1,审批意见2,审批意见3

300 回退1,回退2

时间: 2024-08-03 06:25:39

Oracle 11g 列转行listagg的相关文章

转:Oracle的列转行函数:LISTAGG()

先看示例代码: Sql代码   with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nation ,'New York' city from d

Oracle 列转行函数 Listagg()

这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: Sql代码   with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nat

行转列UNPIVOT、列转行PIVOT,注意是Oracle 11g及以后才支持

现有一表,原始数据如下: 现在需要查询出如下结果(条件是:[80,~]优秀,[60,80)及格,[~,60)不及格): 先用UNPIVOT对原始表进行转换,列转行 select * from CB_SHANGCICB UNPIVOT (cj for kc in ("语文","数学","英语")) ,结果如下: 再对cj进行区间匹配 with tt as (select * from score unpivot (cj for kc in (&qu

Oracle列转行函数版本不兼容解决方案

业务场景 本博客记录一下Oracle列转行函数在Oracle11的一些不兼容问题,vm_concat在一些业务场景是必须的.不过这个函数使用要谨慎,底层实现应该也是group by等等实现的,性能并不是特别好.这个函数在Oracle12是没有的,在Oracle11是不太兼容的,Oracle10可以正常使用.最近遇到这个问题,网上博客很多都写到了自定义列转行函数的办法去解决.但是这种办法并不一定适用所有的业务场景.我并没有采用.不过有些场景还是可以使用的. 网上优秀例子 下面是网络记录比较详细的例

Oracle行转列、列转行的Sql语句总结(转)

多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union all函数等方式 wm_concat函数 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以",&quo

Oracle 行转列、列转行 的Sql语句总结

参考文章:http://blog.csdn.net/tianlesoftware/article/details/4704858 多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union

Oracle 11g pivot行转列

之前写过一篇行转列的文章:Oracle 简单的列转行 SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2.0.1.0 -

Oracle行转列、列转行的Sql语句总结

多行转字符串 这个比较简单,用||或concat函数可以实现 ?SQL Code? 12 ? select?concat(id,username)?str?from?app_userselect?id||username?str?from?app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union all函数等方式 wm_concat函数 首先让我们来看看这个神奇的函数wm_concat(列名)

Oracle中的列转行例子详解

数据如下:name id张三 1,2,3 要求实现:name id张三 1张三 2张三 3 --创建临时表 create table tmp as(select '张三' name, '1,2,3' id from dual); --写法1 select name,regexp_substr(id,'[^,]+',1,level) as id from tmp connect by level <= regexp_count(id,',')+1 order by id --写法2: select