手机版
你好,游客 登录 注册
背景:
阅读新闻

Oracle函数wmsys.wm_concat的使用

[日期:2012-04-25] 来源:Linux社区  作者:psyuhen [字体:大 中 小]

首先介绍语法:

wmsys.wm_concat

Definition:
The Oracle PL/SQL WM_CONCAT function is used to aggregate data from a number of rows into a single row, giving a list of data associated with a specific value. In effect, it cross-tabulates a comma delimited list.

Note that WM_CONCAT is undocumented and unsupported by Oracle, meaning it should not be used in production systems. The LISTAGG function, which can produce the same output asWM_CONCAT is both documented and supported by Oracle.

例子:

  1. select t1.main_id,  
  2.        to_char(wmsys.wm_concat(t1.send_id || '||' || t2.realname || '||' ||  
  3.                                t2.IMG_PATH)) sendidlist  
  4.   from LCMS_SEND_USER t1, LCMS_USER_STUD t2  
  5.  group by t1.main_id;  

结果:

网络中也有这样使用:

下面只是收藏:

  1. SQL> select id,wmsys.wm_concat(name) over (order by id) name from idtable;  
  2.   
  3.         ID NAME  
  4. ---------- --------------------------------------------------------------------------------   
  5.         10 ab,bc,cd  
  6.         10 ab,bc,cd  
  7.         10 ab,bc,cd  
  8.         20 ab,bc,cd,hi,ij,mn  
  9.         20 ab,bc,cd,hi,ij,mn  
  10.         20 ab,bc,cd,hi,ij,mn  
  11.   
  12. 6 rows selected  


  1. SQL> select id,wmsys.wm_concat(name) over (order by id,name) name from idtable;  
  2.   
  3.         ID NAME  
  4. ---------- --------------------------------------------------------------------------------   
  5.         10 ab  
  6.         10 ab,bc  
  7.         10 ab,bc,cd  
  8.         20 ab,bc,cd,hi  
  9.         20 ab,bc,cd,hi,ij  
  10.         20 ab,bc,cd,hi,ij,mn  
  11.   
  12. 6 rows selected  
  13.   
  14. 个人觉得这个用法比较有趣.  
  15.   
  16. SQL> select id,wmsys.wm_concat(name) over (partition by id) name from idtable;  
  17.   
  18.         ID NAME  
  19. ---------- --------------------------------------------------------------------------------   
  20.         10 ab,bc,cd  
  21.         10 ab,bc,cd  
  22.         10 ab,bc,cd  
  23.         20 hi,ij,mn  
  24.         20 hi,ij,mn  
  25.         20 hi,ij,mn  
  26.   
  27. 6 rows selected  
  28.   
  29. SQL> select id,wmsys.wm_concat(name) over (partition by id,name) name from idtable;  
  30.   
  31.         ID NAME  
  32. ---------- --------------------------------------------------------------------------------   
  33.         10 ab  
  34.         10 bc  
  35.         10 cd  
  36.         20 hi  
  37.         20 ij  
  38.         20 mn  
  39.   
  40. 6 rows selected  

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

linux
相关资讯       Oracle函数 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款