Oracle行转列(使用pivot函数)

在日常使用中,经常遇到这样的情况,需要将数据库中行转化成列显示,如

转化为

这个时候,我们就需要使用pivot函数

百度后,参考网址http://www.2cto.com/database/201501/367164.html ,完成了以下操作

with temp as(

select ‘四川省‘ nation ,‘成都市‘ city,‘第一‘ ranking from dual union all

select ‘四川省‘ nation ,‘绵阳市‘ city,‘第二‘ ranking from dual union all

select ‘四川省‘ nation ,‘德阳市‘ city,‘第三‘ ranking from dual union all

select ‘四川省‘ nation ,‘宜宾市‘ city,‘第四‘ ranking from dual union all

select ‘湖北省‘ nation ,‘武汉市‘ city,‘第一‘ ranking from dual union all

select ‘湖北省‘ nation ,‘宜昌市‘ city,‘第二‘ ranking from dual union all

select ‘湖北省‘ nation ,‘襄阳市‘ city,‘第三‘ ranking from dual

)

select * from (select nation,city,ranking from temp)pivot (max(city) for ranking in (‘第一‘ as 第一,‘第二‘ AS 第二,‘第三‘ AS 第三,‘第四‘ AS 第四));

这样就顺利的实现了操作,其中关键函数pivot,其用法如下

pivot(聚合函数 for 列名 in(类型))

--其中 in(‘’) 中可以指定别名,in中还可以指定子查询,比如 select distinct ranking from temp

当然也可以不使用pivot函数,使用下面的语句同样可以实现效果

with temp as(

select ‘四川省‘ nation ,‘成都市‘ city,‘第一‘ ranking from dual union all

select ‘四川省‘ nation ,‘绵阳市‘ city,‘第二‘ ranking from dual union all

select ‘四川省‘ nation ,‘德阳市‘ city,‘第三‘ ranking from dual union all

select ‘四川省‘ nation ,‘宜宾市‘ city,‘第四‘ ranking from dual union all

select ‘湖北省‘ nation ,‘武汉市‘ city,‘第一‘ ranking from dual union all

select ‘湖北省‘ nation ,‘宜昌市‘ city,‘第二‘ ranking from dual union all

select ‘湖北省‘ nation ,‘襄阳市‘ city,‘第三‘ ranking from dual

)

 

select nation,

max(decode(ranking, ‘第一‘, city, ‘‘)) as 第一,

max(decode(ranking, ‘第二‘, city, ‘‘)) as 第二,

max(decode(ranking, ‘第三‘, city, ‘‘)) as 第三,

max(decode(ranking, ‘第四‘, city, ‘‘)) as 第四

from temp group by nation;

当然Oracle还提供了unpivot函数,实现列转换的操作,项目中还没有使用,也就没有细细研究了。

参考地址:http://blog.csdn.net/xb12369/article/details/39554935

解决长久以来遇到的问题,心情舒畅,遂记录于此,方便后来查看

时间: 2024-08-19 13:29:59

Oracle行转列(使用pivot函数)的相关文章

SQL Server 2008 R2——PIVOT 行转列 以及聚合函数的选择

原文:SQL Server 2008 R2--PIVOT 行转列 以及聚合函数的选择 ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完整性. 未经作者同意请勿修改(包括本声明),保留法律追究的权利. 未经作者同意请勿用于学术性引用. 未经作者同意请勿用于商业出版.商业印刷.商业引用. 本文不定期修正完善,为保证内容正确,建议移步原文处阅读. 本文

oracle行转列

针对oracle数据查询的数据,行转列 1.wm_concat函数: 例一: select c1,c2,wm_concat(c3) from T where.... group by c1,c2 查询结果自动用","分割 例二: select c1,c2,wm_concat(c3)over(partition by ..order by..) from T where.... 其中,partition用来分组, order用来排序 2.sys_connect_by_path函数: 这个

oracle 行转列 分析函数

oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + 1 || ':' || ELEMENT) ELEMENT FROM T_MERCH_ELEMENT where code_ts='020745' group by CODE_TS; 得到的结果: 上面大家可能会发现序号没有按顺序排列下来.如果没有要求,就这样就可以了.如果要排序看方法二. 方法二:WM_CONC

oracle行转列和列转行(pivot 和 unpivot 函数,wm_concat函数 )

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, '橘子',

Oracle行转列的函数

--行转列的函数-- CREATE OR REPLACE FUNCTION Calvin( col IN VARCHAR2,dw IN VARCHAR2) RETURN VARCHAR2 IS retval varchar2(32); Sel_sql varchar2(2000); ---//SQL语句声明 BEGIN Sel_sql:='select '||col||' from TB5001 where dwdm='''|| dw||''''; execute immediate Sel_s

Oracle 行转列(pivot、wm_concat、decode)使用总结(转载)

偶然需要了解,学习了这篇文章,转载记录一下 自:http://blog.csdn.net/jxzkin/article/details/7949629 1.创建测试数据 [html]?view plaincopy CREATE?TABLE?CC?? ??(Student?NVARCHAR2(2),Course?NVARCHAR2(2),Score?INT?? ??);?? [html]?view plaincopy INSERT?into?CC??? select?N'张三',N'语文',78?

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

多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union all函数等方式 wm_concat函数 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以",&quo

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

参考文章:http://blog.csdn.net/tianlesoftware/article/details/4704858 多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union

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

多行转字符串 这个比较简单,用||或concat函数可以实现 ?SQL Code? 12 ? select?concat(id,username)?str?from?app_userselect?id||username?str?from?app_user 字符串转多列 实际上就是拆分字符串的问题,可以使用 substr.instr.regexp_substr函数方式 字符串转多行 使用union all函数等方式 wm_concat函数 首先让我们来看看这个神奇的函数wm_concat(列名)