wm_concat函数 用法(神奇的函数)

wm_concat函数 用法

wm_concat函数

上次我说用到了wm_xoncat函数,那么这篇文章,让我们探索一下这个神奇的函数吧,各位小伙伴!


  1. 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用  
  2. 准备测试数据  
  3. SQL> create table test(id number,name varchar2(20));  
  4. SQL> insert into test values(1,'a');  
  5. SQL> insert into test values(1,'b');  
  6. SQL> insert into test values(1,'c');  
  7. SQL> insert into test values(2,'d');  
  8. SQL> insert into test values(2,'e');  
  9. SQL> commit;  
  10. 效果1 : 行转列  
  11. SQL> select wm_concat(namefrom test;  
  12. WM_CONCAT(NAME)  
  13. -------------------------------------------------------------------------  
  14. a,b,c,d,e  
  15. 效果2: 把结果里的逗号替换成"|"  
  16. SQL> select replace(wm_concat(name),',','|'from test;  
  17. REPLACE(WM_CONCAT(NAME),',','|')  
  18. -----------------------------------------------------------------------  
  19. a|b|c|d|e  
  20. 效果3:按ID分组合并name  
  21. SQL> select id,wm_concat(namename from test group by id;  
  22. ID NAME  
  23. ---------- ------------------------------  
  24. 1 a,b,c  
  25. 2 d,e  
  26. 懒人扩展用法:  
  27. 案例:我要写一个视图,类似"create or replace view as select 字段1,...字段50 from tablename" ,基表有50多个字段,要是靠手工写太麻烦了,有没有什么简便的方法? 当然有了,看我如果应用wm_concat来让这个需求变简单  
  28. SQL> select 'create or replace view as select '|| wm_concat(column_name) || ' from dept'from user_tab_columns where table_name='DEPT';  
  29. 'CREATEORREPLACEVIEWASSELECT'||WM_CONCAT(COLUMN_NAME)||'FROMDEPT'  
  30. --------------------------------------------------------------------------------  
  31. create or replace view as select DEPTNO,DNAME,LOC from dept  
  • 发表于 2017-11-17 19:26
  • 阅读 ( 1604 )
  • 分类:Java

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
不写代码的码农
江南烟雨

可以跨境界的java开发攻城狮

36 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章