周郑

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

发布时间:7年前浏览量: 2230 ℃评论数:暂未开放


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

 SQL Code 

1
2

  

select concat(id,username) str from app_user
select id||username str from app_user

字符串转多列

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

字符串转多行

使用union all函数等方式

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;

   

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

 SQL Code 

1

  

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

   

效果3: 按ID分组合并name

 SQL Code 

1

  

select id,wm_concat(name) name from test group by id;

   

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';

   

利用系统表方式查询

 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<

oracle,转行,语句,总结

微信扫码打赏

打赏排行榜

| 施主 | 时间 | 方式 | 金额 |

曾 俊 2020-02-16 微信打赏 ¥10.00

王 强 2020-01-24 微信打赏 ¥5.00

莫国成 2019-07-20 微信打赏 ¥ 50.00

说明:打赏记录每日24:00:00更新,50%用于无名公益!

1 2 3

手机扫码访问