zoukankan      html  css  js  c++  java
  • oracle 分组拼接

      方法一:listagg, 参考链接,从oracle11g后出现的新函数

      如果拼接的字符串长度超过4000字节,会报ora-01489错误,ora-01489 字符串连接的结果过长 解决方案

    SELECT 
    	id,count(name) as countname,
    	LISTAGG(to_char(name), ',') WITHIN GROUP(ORDER BY name) AS pjname
    FROM 
    (
    	select 
    		distinct B.id as id, 
    		B.name as name
    	from 
    		B left join C on B.name = C.name
    	where 
    		C.gender = 'female'
    )
    group by id
    

      方法二:wmsys.wm_concat参考链接,返回结果类型会受版本影响,不建议使用

    select 
    	grp, wmsys.wm_concat(str)
    from 
    (
    	select 1 grp, 'a1' str from dual
        union
        select 1 grp, 'a2' str from dual
        union
        select 2 grp, 'b1' str from dual
        union
        select 2 grp, 'b2' str from dual
        union
        select 2 grp, 'b3' str from dual
    ) t
    group by
    	grp
    

      方法三(1):sys_connect_by_path参考链接

    select role_category,  
           ltrim(sys_connect_by_path(role_id, ','), ',') as role_id_list  
    from   (  
            select role_category,  
                   role_id,  
                   (row_number() over(partition by role_category order by role_id))  
                      as row_number  
            from   (  
                    select 'role_01' as role_id ,'admin' as role_category from dual  
                    union all  
                    select 'role_02' as role_id ,'admin' as role_category from dual  
                    union all  
                    select 'role_03' as role_id ,'normal' as role_category from dual  
                    union all  
                    select 'role_04' as role_id ,'normal' as role_category from dual  
                    union all  
                    select 'role_05' as role_id ,'normal' as role_category from dual  
                    union all  
                    select 'role_06' as role_id ,'normal' as role_category from dual  
                    union all  
                    select 'role_07' as role_id ,'normal' as role_category from dual  
                    union all  
                    select 'role_08' as role_id ,'normal' as role_category from dual  
                   )  
           )  
    where  connect_by_isleaf = 1  
           start with row_number = 1  
           connect by row_number - 1 = prior row_number  
           and role_category = prior role_category
    

      方法三(2):sys_connect_by_path参考链接

    select 
    	deptno,
    	ltrim(max(sys_connect_by_path(ename, ',')), ',') name
    from
    (
    	select 
    		deptno, 
    		ename, 
    		row_number() over(partition by deptno order by ename) ro
        from 
    	(
    		select 
    			deptno,ename
    		from 
    			emp 
    		where 
    			deptno is not null
    	) t
    ) newtab
    start with newtab.ro = 1
    connect by prior newtab.ro = newtab.ro - 1
    group by deptno;
    

      方法三(3):sys_connect_by_path参考链接

    select 
    	deptno,
    	ltrim(max(sys_connect_by_path(ename, ',')), ',') name
    from
    (
    	select 
    		deptno, 
    		ename, 
    		ro,
    		lead(ro) over(partition by deptno order by ro) ro_next
        from 
    	(
    		select 
    			deptno, ename,
    			row_number() over(order by deptno, ename) ro
    		from 
    			emp 
    		where 
    			deptno is not null
    	) t
    ) newtab
    start with ro_next is null
    connect by ro_next = prior ro
    group by deptno;
    

      

  • 相关阅读:
    Java 项目运用个人看法(简写)
    windows 搭建Solr连接数据库
    总结2016年,计划2017
    如何解决,自己认为特别难的问题?(文摘)
    spring -quartz 定时任务多任务配置
    (转) java Timer 定时每天凌晨1点执行任务
    spring多数据源切换,写入报错的问题
    如何合理和有效的进行数据库设计
    Main方法定点执行线程任务
    莫辜负当下,莫悔恨过去,莫打扰错过的人
  • 原文地址:https://www.cnblogs.com/iupoint/p/10968405.html
Copyright © 2011-2022 走看看