ORACLE wm_concat (wmsys.) 函数(行列转换函数)

简介:

SQL wm_concat function
Expert Oracle Tips by Burleson Consulting

September 19, 2010

Question: I have a table test_test and I need to count the distinct mark columns and them display all matching values on one line:

Name Mark
------- ------
ABC 10
DEF 10
GHI 10
JKL 20
MNO 20
PQR 30

The result should be like tghis, with the count and the rows groups onto the same line;
mark count     names
---- -----     -----------
10       3     ABC,DEF,GHI
20       2     JKL,MNO
30       1     PQR

Answer: By Laurent Schneider:  You could write your own aggregate function or use WM_CONCAT:

select 
   mark, 
   count(*), 
   wm_concat(name) 
from 
   test_test 
group by 
   mark;

Here is another example of using wm_contcat:

select 
   deptno, 
   wm_concat(distinct ename) 
from 
   emp 
group by 
   deptno;

DEPTNO     WM_CONCAT(DISTINCTENAME)
---------- ----------------------------------------
10         CLARK,KING,MILLER
20         ADAMS,FORD,JONES,SCOTT,SMITH
30         ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD

Also see:

pivoting one row of several columns into one column of several rows.
Display and sort output on a single line

 

SQL>                                   
SQL> select mgr ,                      
  2  wm_concat(ename) names,           
  3  count(*)CNT                       
  4  from scott.emp                    
  5  group by mgr                      
  6  ;                                 
                                       
  MGR NAMES                              CNT
----- -------------------------------  -----
 7566 SCOTT,FORD                           2
 7698 ALLEN,WARD,TURNER,JAMES,MARTIN       5
 7782 MILLER                               1
 7788 ADAMS                                1
 7839 JONES,CLARK,BLAKE                    3
 7902 SMITH                                1
      KING                                 1
                                       
7 rows selected                        

本文转自海天一鸥博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2011/01/24/1943165.html,如需转载请自行联系原作者
相关文章
|
4月前
|
SQL Oracle 关系型数据库
Oracle之日期计算相关函数
Oracle之日期计算相关函数
45 0
|
4月前
|
SQL Oracle 关系型数据库
Oracle之regexp系列函数详解
Oracle之regexp系列函数详解
148 1
|
3月前
|
存储 Java 数据库
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(二)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
38 0
|
3月前
|
SQL Oracle 关系型数据库
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
67 0
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
|
7天前
|
Oracle 算法 关系型数据库
Oracle常用系统函数之数字类函数:数字的魔术师
【4月更文挑战第19天】Oracle数据库中的数字类函数是数字处理的魔术师,包括`ROUND`(四舍五入),`CEIL`和`FLOOR`(向上/下取整),以及`ABS`(计算绝对值)。还有`MOD`、`TRUNC`和`POWER`等函数,提供求余数、截断和计算幂的功能。熟练运用这些函数能提升数据管理效率,让处理数字变得更简单、有趣。
|
2月前
|
SQL Oracle 关系型数据库
Oracle查询优化-聚集函数
【2月更文挑战第5天】【2月更文挑战第13篇】聚集函数
19 4
|
3月前
|
存储 SQL Java
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(一)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
35 0
|
3月前
|
SQL Oracle 算法
Oracle函数
Oracle函数
98 1
|
4月前
|
SQL Oracle 关系型数据库
Oracle之有哪些日期计算函数?
Oracle之有哪些日期计算函数?
114 0
|
4月前
|
SQL Oracle 关系型数据库
Oracle之常用聚集函数详解
Oracle之常用聚集函数详解
40 1