OraCle 记录 实现 sql中的 for xml path ('')

select  * from qx_role where N_role_id in ( 1,2,3)

运行结果:

select to_char(wm_concat(c_role_name)) from qx_role
where N_role_id in ( SELECT REGEXP_SUBSTR ( '1,2,3', '[^,]+', 1,rownum)
from dual connect by rownum<=LENGTH ( '1,2,3') - LENGTH (regexp_replace( '1,2,3', ',', ''))+1)
实现sql

运行结果:

  wm_concat(col) 方法 可当做 sum 等函数 可配合group By 使用  此方法可以将每一行的  col 值 以逗号的形式 拼接成一个值 与  sql server 中的for xml path('') 相似;

 SELECT REGEXP_SUBSTR ( '1,2,3', '[^,]+', 1,rownum)
from dual connect by rownum<=LENGTH ( '1,2,3') - LENGTH (regexp_replace( '1,2,3', ',', ''))+1

效果:

 这条 sql 主要 是实现了 把  字符串 “1,2,3” 转换成表结构  相当于 前面 sql 里面的表值函数 s_split() 

初接触oracle  做记录  关于 connect by 的用法 可参考收藏博客  start with connect by prior 递归查询用法

select t.c_user_id,
               to_char(wm_concat(t.c_situation_use_month)
                       over(partition by c_user_id order by
                            t.c_situation_use_month)) qfyf,
               t.n_amount qfje,
               t.c_situation_use_month qfy
          from yx_situation t
         where t.n_calculation_way = 0
           and t.n_charge_state = 0
运用

运行效果:

 查询语句中配合 over() 函数开窗;

原文地址:https://www.cnblogs.com/pyf97/p/13098032.html