多行转字符串
这个比较简单,用||或concat函数可以实现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(列名),该函数可以把列值以","号分隔起来,并显示成一行,接下来上例子,看看这个神奇的函数如何应用准备测试数据
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 : 行转列 ,默认逗号隔开
select wm_concat(name) name from test;
效果2: 把结果里的逗号替换成"|"
select replace(wm_concat(name),',','|') from test;
效果3: 按ID分组合并name
select id,wm_concat(name) name from test group by id;
sql 语句等同于下面的sql语句
-------- 适用范围:8i,9i,10g及以后版本 ( MAX + DECODE )
select id, <span id="14_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=max&k0=max&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="14" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">max</span></a></span>(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 <span id="15_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="15_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=group&k0=group&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="15" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">group</span></a></span> 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 || <span id="16_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="16_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=lead&k0=lead&kdi0=0&luki=5&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="16" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">lead</span></a></span>(',' || 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,<span id="17_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="17_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=max&k0=max&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="17" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">max</span></a></span>(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 <span id="18_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="18_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=group&k0=group&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="18" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">group</span></a></span> by t.id;</span>
懒人扩展用法:
案例: 我要写一个视图,类似"create or replace view as select 字段1,...字段50 from tablename" ,基表有50多个字段,要是靠手工写太麻烦了,有没有什么简便的方法? 当然有了,看我如果应用wm_concat来让这个需求变简单,假设我的APP_USER表中有(id,username,password,age)4个字段。查询结果如下
/** 这里的表名默认区分大小写 */
select 'create or replace view as select '|| wm_concat(column_name) || ' from APP_USER' <span id="13_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=sql&k0=sql&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="13" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">sql</span></a></span>Str from user_tab_columns where table_name='APP_USER';
利用系统表方式查询
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,类型名称,销售数量),案例:根据水果的类型查询出一条数据显示出每种类型的销售数量。
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);
分组查询 (当然这是不符合查询一条数据的要求的)
select name, <span id="10_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=sum&k0=sum&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="10" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">sum</span></a></span>(nums) nums <span id="11_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=from&k0=from&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="11" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">from</span></a></span> demo <span id="12_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=group&k0=group&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="12" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">group</span></a></span> by name
行转列查询
select * from (select name, nums from demo) pivot (sum(nums) for name in ('苹果' 苹果, '橘子', '<span id="9_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=%C6%CF%CC%D1&k0=%C6%CF%CC%D1&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="9" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">葡萄</span></a></span>', '芒果'));
注意: pivot(聚合函数 for 列名 in(类型)) ,其中 in(‘’) 中可以指定别名
当然也可以不使用pivot函数,等同于下列语句,只是代码比较长,容易理解
select * from (select <span id="6_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=sum&k0=sum&kdi0=0&luki=9&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="6" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">sum</span></a></span>(nums) 苹果 <span id="7_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=from&k0=from&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="7" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">from</span></a></span> 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个季度的销售数量,现在要将每种水果的每个季度的销售情况用多行数据展示。
创建表和数据
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,'<span id="5_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=%C6%CF%CC%D1&k0=%C6%CF%CC%D1&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="5" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">葡萄</span></a></span>',1500,2500,1200,3500);
select * from Fruit
列转行查询
select id , name, jidu, xiaoshou <span id="4_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=from&k0=from&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="4" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">from</span></a></span> Fruit unpivot (xiaoshou for jidu in (q1, q2, q3, q4) )
注意: unpivot没有聚合函数,xiaoshou、jidu字段也是临时的变量
同样不使用unpivot也可以实现同样的效果,只是sql语句会很长,而且执行速度效率也没有前者高
select id, name ,'Q1' jidu, (select q1 <span id="0_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=from&k0=from&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="0" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">from</span></a></span> <span id="1_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=fruit&k0=fruit&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="1" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">fruit</span></a></span> where id=f.id) xiaoshou from Fruit f
<span id="2_nwp" style="width: auto; height: auto; float: none;"><a target=_blank id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=4f181f334ed3f7aa&k=union&k0=union&kdi0=0&luki=4&mcpm=0&n=10&p=baidu&q=35027180_cpr&rb=0&rs=1&seller_id=1&sid=aaf7d34e331f184f&ssp2=1&stid=9&t=tpclicked3_hc&td=2282542&tu=u2282542&u=http%3A%2F%2Fwww%2Ezgxue%2Ecom%2F168%2F1682828%2Ehtml&urlid=0" target="_blank" mpid="2" style="color: rgb(51, 51, 51); text-decoration: none;"><span style="color: rgb(0, 0, 255); width: auto; height: auto;">union</span></a></span>
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