wm_concat函数
首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用准备测试数据
SQL Code
1 2 3 4 5 6 | | create table test(id number,name varchar2(20)); insert into test values(1,'a'); insert into test values(1,'b'); insert into test values(1,'c'); insert into test values(2,'d'); insert into test values(2,'e'); |
效果1 : 行转列 ,默认逗号隔开
SQL Code
1 | | select wm_concat(name) name from test; |
![](https://i-blog.csdnimg.cn/blog_migrate/7a96c4cc8c3a2a0dfa614ab14c1d26ab.png)
效果2: 把结果里的逗号替换成"|"
SQL Code
1 | | select replace(wm_concat(name),',','|') from test; |
![](https://i-blog.csdnimg.cn/blog_migrate/003163d2f0bd2ac1d129752d3114a619.png)
效果3: 按ID分组合并name
SQL Code
1 | | select id,wm_concat(name) name from test group by id; |
![](https://i-blog.csdnimg.cn/blog_migrate/00dd5fd8f471c0396e0e1543a335a2f7.png)
sql语句等同于下面的sql语句:
SQL Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | | -------- 适用范围:8i,9i,10g及以后版本 ( MAX + DECODE ) select id, max(decode(rn, 1, name, null)) || max(decode(rn, 2, ',' || name, null)) || max(decode(rn, 3, ',' || name, null)) str from (select id, name, row_number() over(partition by id order by name) as rn from test) t group by id order by 1; -------- 适用范围:8i,9i,10g及以后版本 ( ROW_NUMBER + LEAD ) select id, str from (select id, row_number() over(partition by id order by name) as rn, name || lead(',' || name, 1) over(partition by id order by name) || lead(',' || name, 2) over(partition by id order by name) || lead(',' || name, 3) over(partition by id order by name) as str from test) where rn = 1 order by 1; -------- 适用范围:10g及以后版本 ( MODEL ) select id, substr(str, 2) str from test model return updated rows partition by(id) dimension by(row_number() over(partition by id order by name) as rn) measures(cast(name as varchar2(20)) as str) rules upsert iterate(3) until(presentv(str [ iteration_number + 2 ], 1, 0) = 0) (str [ 0 ] = str [ 0 ] || ',' || str [ iteration_number + 1 ]) order by 1; -------- 适用范围:8i,9i,10g及以后版本 ( MAX + DECODE ) select t.id id, max(substr(sys_connect_by_path(t.name, ','), 2)) str from (select id, name, row_number() over(partition by id order by name) rn from test) t start with rn = 1 connect by rn = prior rn + 1 and id = prior id group by t.id; |
懒人扩展用法:
案例: 我要写一个视图,类似"create or replace view as select 字段1,...字段50 from tablename" ,基表有50多个字段,要是靠手工写太麻烦了,有没有什么简便的方法? 当然有了,看我如果应用wm_concat来让这个需求变简单,假设我的APP_USER表中有(id,username,password,age)4个字段。查询结果如下
SQL Code
1 2 3 4 5 | | /** 这里的表名默认区分大小写 */ select 'create or replace view as select ' || wm_concat(column_name) || ' from APP_USER' sqlStr from user_tab_columns where table_name = 'APP_USER'; |
![](https://i-blog.csdnimg.cn/blog_migrate/1ce1ca460b4053e107f58b72d452aec2.png)
利用系统表方式查询
SQL Code
1 | | select * from user_tab_columns |
Oracle 11g 行列互换 pivot 和 unpivot 说明
在Oracle 11g中,Oracle 又增加了2个查询:pivot(行转列) 和unpivot(列转行)
参考:http://blog.csdn.net/tianlesoftware/article/details/7060306、http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html
google 一下,网上有一篇比较详细的文档:http://www.oracle-developer.net/display.php?id=506
pivot 列转行
测试数据 (id,类型名称,销售数量),案例:根据水果的类型查询出一条数据显示出每种类型的销售数量。
SQL Code
1 2 3 4 5 6 7 8 9 | | create table demo(id int,name varchar(20),nums int); ---- 创建表 insert into demo values(1, '苹果', 1000); insert into demo values(2, '苹果', 2000); insert into demo values(3, '苹果', 4000); insert into demo values(4, '橘子', 5000); insert into demo values(5, '橘子', 3000); insert into demo values(6, '葡萄', 3500); insert into demo values(7, '芒果', 4200); insert into demo values(8, '芒果', 5500); |
![](https://i-blog.csdnimg.cn/blog_migrate/2b175cfd8d8c15053c99a5323df96b03.png)
分组查询 (当然这是不符合查询一条数据的要求的)
SQL Code
1 | | select name, sum(nums) nums from demo group by name |
![](https://i-blog.csdnimg.cn/blog_migrate/08133cc65289b2c2b8793c1080909005.png)
行转列查询
SQL Code
1 | | select * from (select name, nums from demo) pivot (sum(nums) for name in ('苹果' 苹果, '橘子', '葡萄', '芒果')); |
![](https://i-blog.csdnimg.cn/blog_migrate/3e33adb2cf6a988394af0264961ea476.png)
注意: pivot(聚合函数 for 列名 in(类型)) ,其中 in('') 中可以指定别名,in中还可以指定子查询,比如 select distinct code from customers
当然也可以不使用pivot函数,等同于下列语句,只是代码比较长,容易理解
SQL Code
1 2 3 4 5 | | select * from (select sum(nums) 苹果 from demo where name = '苹果'), (select sum(nums) 橘子 from demo where name = '橘子'), (select sum(nums) 葡萄 from demo where name = '葡萄'), (select sum(nums) 芒果 from demo where name = '芒果'); |
unpivot 行转列
顾名思义就是将多列转换成1列中去
案例:现在有一个水果表,记录了4个季度的销售数量,现在要将每种水果的每个季度的销售情况用多行数据展示。
创建表和数据
SQL Code
1 2 3 4 5 6 | | create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int); insert into Fruit values(1,'苹果',1000,2000,3300,5000); insert into Fruit values(2,'橘子',3000,3000,3200,1500); insert into Fruit values(3,'香蕉',2500,3500,2200,2500); insert into Fruit values(4,'葡萄',1500,2500,1200,3500); select * from Fruit |
![](https://i-blog.csdnimg.cn/blog_migrate/d311589a3d675c3966d580e88df22065.png)
列转行查询
SQL Code
1 | | select id , name, jidu, xiaoshou from Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4) ) |
注意: unpivot没有聚合函数,xiaoshou、jidu字段也是临时的变量
![](https://i-blog.csdnimg.cn/blog_migrate/d728f01a2e35855b806f576ed1e0f273.png)
同样不使用unpivot也可以实现同样的效果,只是sql语句会很长,而且执行速度效率也没有前者高
SQL Code
1 2 3 4 5 6 7 | | select id, name ,'Q1' jidu, (select q1 from fruit where id=f.id) xiaoshou from Fruit f union select id, name ,'Q2' jidu, (select q2 from fruit where id=f.id) xiaoshou from Fruit f union select id, name ,'Q3' jidu, (select q3 from fruit where id=f.id) xiaoshou from Fruit f union select id, name ,'Q4' jidu, (select q4 from fruit where id=f.id) xiaoshou from Fruit f |
XML类型
上述pivot列转行示例中,你已经知道了需要查询的类型有哪些,用in()的方式包含,假设如果您不知道都有哪些值,您怎么构建查询呢?
pivot 操作中的另一个子句 XML 可用于解决此问题。该子句允许您以 XML 格式创建执行了 pivot 操作的输出,在此输出中,您可以指定一个特殊的子句 ANY 而非文字值
示例如下:
SQL Code
1 2 3 4 5 6 7 | | select * from ( select name, nums as "Purchase Frequency" from demo t ) pivot xml ( sum(nums) for name in (any) ) |
![](https://i-blog.csdnimg.cn/blog_migrate/99ad7367055c0566a61e389099cbcb5e.png)
如您所见,列 NAME_XML 是 XMLTYPE,其中根元素是 <PivotSet>。每个值以名称-值元素对的形式表示。您可以使用任何 XML 分析器中的输出生成更有用的输出。
![](https://i-blog.csdnimg.cn/blog_migrate/27f918ccaf76bfc030b1dbf2d79bd058.png)
对于该xml文件的解析,贴代码如下:
SQL Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | | create or replace procedure ljz_pivot_xml_sp(pi_table_name varchar2, pi_column_name varchar2, pi_create_table varchar2) as v_column nvarchar2(50); v_count number := 0; v_i number; v_parent_node nvarchar2(4000); v_child_node nvarchar2(4000); v_over boolean := false; v_tmp nvarchar2(50); v_existsnode number; v_sql clob; v_name varchar2(30); v_name_xml xmltype; begin v_sql := 'select x.* from ' || pi_table_name || ' a, xmltable(''/PivotSet'' passing a.' || pi_column_name || ' columns '; v_parent_node := '/PivotSet'; v_child_node := 'item[1]/column[2]'; v_i := 1; execute immediate 'select ' || pi_column_name || ' from ' || pi_table_name || ' where rownum=1' into v_name_xml; select existsnode(v_name_xml, '/PivotSet/item[' || to_char(v_i) || ']/column[1]') into v_existsnode from dual; while v_existsnode = 1 loop execute immediate 'select substr(extractvalue(' || pi_column_name || ', ''/PivotSet/item[' || to_char(v_i) || ']/column[1]''),1,30) from ' || pi_table_name || ' x' into v_name; v_sql := v_sql || '"' || v_name || '" varchar2(30) path ''item[' || to_char(v_i) || ']/column[2]'','; v_i := v_i + 1; select existsnode(v_name_xml, '/PivotSet/item[' || to_char(v_i) || ']/column[1]') into v_existsnode from dual; end loop; v_sql := trim(',' from v_sql) || ') x'; commit; select count(1) into v_count from user_tab_columns where table_name = upper(pi_create_table); if v_count = 0 then execute immediate 'create table ' || pi_create_table || ' as ' || v_sql; end if; end; |
第一个参数为要解析xml文件所属数据表,第二个参数为要解析xml所存字段,第三个参数存放解析后的数据集。
测试:
begin
ljz_pivot_xml_sp('(select * from (select deptno,sal from emp) pivot xml(sum(sal) for deptno in(any)))',
'deptno_xml',
'ljz_pivot_tmp');
end;
![](https://i-blog.csdnimg.cn/blog_migrate/f6c6c0387ba4e9d728795cd2b017bf4e.png)
初学oracle xml解析,这种方法较为笨拙,一个一个循环列,原型如下:
select extractvalue(name_xml, '/PivotSet/item[1]/column[1]')
from (select * from (select name,nums from demo) pivot xml(sum(nums) for name in(any))) x
where existsnode(name_xml, '/PivotSet/item[1]/column[1]') = 1;
![](https://i-blog.csdnimg.cn/blog_migrate/8bc624bab2f89233d6d75306c43d008a.png)
select x.*
from (select *
from (select name, nums from demo)
pivot xml(sum(nums)
for name in(any))) a,
xmltable('/PivotSet' passing a.name_xml columns
芒果 varchar2(30) path 'item[1]/column[2]',
苹果 varchar2(30) path 'item[2]/column[2]') x
![](https://i-blog.csdnimg.cn/blog_migrate/713028b851788678e5025b5a27fe4bb7.png)
不知是否存在直接进行解析的方法,这种方法还不如直接行列转变,不通过xml转来转去。
select '''' || listagg(substr(name, 1, 30), q'{','}') within group(order by name) || ''''
from (select distinct name from demo);
![](https://i-blog.csdnimg.cn/blog_migrate/5650cadba8eb26fd09af8a99cfc45e3e.png)
select *
from (select name, nums from demo)
pivot(sum(nums)
for name in('苹果', '橘子', '葡萄', '芒果'));
![](https://i-blog.csdnimg.cn/blog_migrate/ec6480bd69976aaac3e2dbb562550cfe.png)
这样拼接字符串反而更加方便。
结论
Pivot 为 SQL 语言增添了一个非常重要且实用的功能。您可以使用 pivot 函数针对任何关系表创建一个交叉表报表,而不必编写包含大量 decode 函数的令人费解的、不直观的代码。同样,您可以使用 unpivot 操作转换任何交叉表报表,以常规关系表的形式对其进行存储。Pivot 可以生成常规文本或 XML 格式的输出。如果是 XML 格式的输出,您不必指定 pivot 操作需要搜索的值域。