Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

多行转字符串

这个比较简单,用||concat函数可以实现

[sql] view plain copy

 print?

1.  select concat(id,username) str from app_user  

2.    

3.  select id||username str from app_user  

字符串转多列

实际上就是拆分字符串的问题,可以使用 substrinstrregexp_substr函数方式

字符串转多行

使用union all函数等方式

 

wm_concat函数

首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用准备测试数据

[sql] view plain copy

 print?

1.  create table test(id number,name varchar2(20));  

2.    

3.  insert into test values(1,'a');  

4.  insert into test values(1,'b');  

5.  insert into test values(1,'c');  

6.  insert into test values(2,'d');  

7.  insert into test values(2,'e');  

效果1 :   行转列  默认逗号隔开

[sql] view plain copy

 print?

1.  select wm_concat(namename from test;  



效果2:   把结果里的逗号替换成"|"

[sql] view plain copy

 print?

1.  select replace(wm_concat(name),',','|'from test;  



效果3:  ID分组合并name

[sql] view plain copy

 print?

1.  select id,wm_concat(namename from test group by id;  

sql语句等同于下面的sql语句

[sql] view plain copy

 print?

1.  -------- 适用范围:8i,9i,10g及以后版本   MAX + DECODE   

2.  select id, max(decode(rn, 1, namenull)) || max(decode(rn, 2, ','||namenull)) || max(decode(rn, 3, ','||namenull)) str  

3.      from (select id, name ,row_number() over(partition by id order by nameas rn from test) t group by id order by 1;   

4.         

5.  -------- 适用范围:8i,9i,10g及以后版本  ROW_NUMBER + LEAD   

6.  select id, str from (select id,row_number() over(partition by id order by nameas rn,name || lead(',' || name, 1)  

7.      over(partition by id order by name) ||  lead(',' || name, 2) over(partition by id order by name) || lead(',' || name, 3)   

8.      over(partition by id order by nameas str from test) where rn = 1 order by 1;  

9.      

10. -------- 适用范围:10g及以后版本  MODEL   

11. select id, substr(str, 2) str from test model return updated rows partition by(id) dimension by(row_number()  

12.     over(partition by id order by nameas rn) measures (cast(name as varchar2(20)) as str) rules upsert iterate(3)  

13.     until(presentv(str[iteration_number + 2], 1, 0)=0) (str[0] = str[0] || ',' || str[iteration_number + 1]) order by 1;       

14.            

15. -------- 适用范围:8i,9i,10g及以后版本  MAX + DECODE   

16. select t.id id, max(substr(sys_connect_by_path(t.name','), 2)) str from (select id, name, row_number()   

17.     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  

18.     group by t.id;  

懒人扩展用法:

案例我要写一个视图,类似"create or replace view as select 字段1,...字段50 from tablename",基表有50多个字段,要是靠手工写太麻烦了,有没有什么简便的方法? 当然有了,看我如果应用wm_concat来让这个需求变简单,假设我的APP_USER表中有(id,username,password,age4个字段。查询结果如下

[sql] view plain copy

 print?

1.   /** 这里的表名默认区分大小写 */  

2.  select 'create or replace view as select '|| wm_concat(column_name) || ' from APP_USER' sqlStr   

3.         from user_tab_columns where table_name='APP_USER';    



利用系统表方式查询

[sql] view plain copy

 print?

1.  select * from user_tab_columns  




Oracle 11g 行列互换pivot unpivot 说明

Oracle 11g中,Oracle 又增加了2个查询:pivot(行转列) unpivot(列转行)

参考:http://blog.csdn.NET/tianlesoftware/article/details/7060306http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html

google 一下,网上有一篇比较详细的文档:http://www.oracle-developer.net/display.php?id=506

pivot 列转行

测试数据 (id,类型名称,销售数量),案例:根据水果的类型查询出一条数据显示出每种类型的销售数量。

[sql] view plain copy

 print?

1.  create table demo(id int,name varchar(20),nums int);  ---- 创建表  

2.  insert into demo values(1, '苹果', 1000);  

3.  insert into demo values(2, '苹果', 2000);  

4.  insert into demo values(3, '苹果', 4000);  

5.  insert into demo values(4, '橘子', 5000);  

6.  insert into demo values(5, '橘子', 3000);  

7.  insert into demo values(6, '葡萄', 3500);  

8.  insert into demo values(7, '芒果', 4200);  

9.  insert into demo values(8, '芒果', 5500);  

分组查询当然这是不符合查询一条数据的要求的)

[sql] view plain copy

 print?

1.  select namesum(nums) nums from demo group by name  

行转列查询

[sql] view plain copy

 print?

1.  select * from (select name, nums from demo) pivot (sum(nums) for name in ('苹果' 苹果, '橘子''葡萄''芒果'));  


注意 pivot(聚合函数 for 列名 in(类型))   ,其中 in(‘’) 中可以指定别名,in中还可以指定子查询,比如 select distinctcode from customers

当然也可以不使用pivot函数,等同于下列语句,只是代码比较长,容易理解

[sql] view plain copy

 print?

1.  ------ 多项子查询  

2.  select * from (select sum(nums) 苹果 from demo where name='苹果'),(select sum(nums) 橘子 from demo where name='橘子'),  

3.         (select sum(nums) 葡萄 from demo where name='葡萄'),(select sum(nums) 芒果 from demo where name='芒果');  

4.           

5.  ------  decode 函数利用  

6.  select sum(decode(name,'苹果',nums)) 苹果, sum(decode(name,'橘子',nums)) 橘子,   

7.         sum(decode(name,'葡萄',nums)) 葡萄, sum(decode(name,'芒果',nums)) 芒果 from demo  

unpivot 行转列

顾名思义就是将多列转换成1列中去
案例:现在有一个水果表,记录了4个季度的销售数量,现在要将每种水果的每个季度的销售情况用多行数据展示。

创建表和数据

[sql] view plain copy

 print?

1.  create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);  

2.    

3.  insert into Fruit values(1,'苹果',1000,2000,3300,5000);  

4.  insert into Fruit values(2,'橘子',3000,3000,3200,1500);  

5.  insert into Fruit values(3,'香蕉',2500,3500,2200,2500);  

6.  insert into Fruit values(4,'葡萄',1500,2500,1200,3500);  

7.  select * from Fruit  



列转行查询

[sql] view plain copy

 print?

1.  select id , name, jidu, xiaoshou from Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4) )  

注意:  unpivot没有聚合函数,xiaoshoujidu字段也是临时的变量


同样不使用unpivot也可以实现同样的效果,只是sql语句会很长,而且执行速度效率也没有前者高

[sql] view plain copy

 print?

1.  select id, name ,'Q1' jidu, (select q1 from fruit where id=f.id) xiaoshou from Fruit f  

2.  union  

3.  select id, name ,'Q2' jidu, (select q2 from fruit where id=f.id) xiaoshou from Fruit f  

4.  union  

5.  select id, name ,'Q3' jidu, (select q3 from fruit where id=f.id) xiaoshou from Fruit f  

6.  union  

7.  select id, name ,'Q4' jidu, (select q4 from fruit where id=f.id) xiaoshou from Fruit f  

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值