Oracle行转列(使用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 * from (select nation,city,ranking from temp)pivot (max(city) for ranking in ('第一' as 第一,'第二' AS 第二,'第三' AS 第三,'第四' AS 第四));pivot(聚合函数 for 列名 in(类型))--其中 in(‘’) 中可以指定别名,in中还可以指定子查询,比如 select distinct ranking from tempwith 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行转列(使用pivot函数)的更多相关文章
- Oracle行转列,pivot函数和unpivot函数
pivot函数:行转列函数: 语法:pivot(任一聚合函数 for 需专列的值所在列名 in (需转为列名的值)):unpivot函数:列转行函数: 语法:unpivot(新增值所在列的列名 for ...
- Oracle 行转列pivot 、列转行unpivot 的Sql语句总结
这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...
- Oracle 行转列 动态出转换的列
本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...
- oracle 行转列 分析函数
oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...
- oracle 行转列函数pivot和unpivot
今天接到业务部门的一个需求,需要对同一公司的不同财务指标进行排序,需要用到oracle的行转列函数unpivot. 财务报表的表结构为: 要实现业务部门的排序筛选功能,需要首先将行数据转为列数据: 使 ...
- oracle行转列和列转行(pivot 和 unpivot 函数,wm_concat函数 )
create table demo(id int,name varchar(20),nums int); ---- 创建表insert into demo values(1, '苹果', 1000); ...
- 列转行pivot函数在SQL Sever里面和Oracle里面的用法区别
首先pivot是一个列转行的函数,反向用是unpivot(行转列). 在SQL sever中可以这么写 SELECT * FROM [TABLE] /*数据源*/ AS A PIVOT ( MAX/* ...
- SQL Server 2008 R2——PIVOT 行转列 以及聚合函数的选择
==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...
- oracle行转列(连接字符串函数)
方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面的参数是列,而且可以是多个列的集 ...
随机推荐
- python中的内存管理
不像大多数编译型语言,变量必须在使用之前声明名字和类型,在python中,变量在第一次被赋值时自动声明.在变量创建时,python解释器会根据语法和右侧的操作数来决定新对象的类型,在对象创建后,一个该 ...
- Productivity Power Tools 动画演示(转)
Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率.它的出现一定程度上弥补和完善了 Visual Studio 自身的不足, ...
- Hive的安装
Hive的安装 第一步:解压并安装:第二步:配置 1)root用户下,解压后,改名为hive,并将hive文件夹赋给hadoop用户 tar -zxvf hive-0.9.0.tar.gz -C ...
- gtktree和gtktext使用时要在文件中定义GTK_ENABLE_BROKEN
linux下调试程序,出现如下错误: /tmp/ccG8fpwg.o: In function `apache_viewlog_form':apache.c:(.text+0x776): undefi ...
- OpenCV Template Matching Subpixel Accuracy
OpenCV has function matchTemplate to easily do the template matching. But its accuracy can only reac ...
- mysql5.7忘记密码
注意:mysql5.7 user表密码字段由password改为authentication_string 1.service mysql stop 2.mysqld_safe --skip-gran ...
- CentOS7上搭建WEB服务器
mysql 安装 直接yum install mysql-server是不可以的 1 wget http://repo.mysql.com/mysql-community-release-el7-5. ...
- 预加载 img 的方式。
原生方式: var img = new Image(); img.onerror = function() { alert('img error'); }; img.onload = function ...
- DOM Document
1.DOM Document对象 定义:每个载入浏览器的 HTML 文档都会成为 Document 对象.Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. Docume ...
- c#委托和事件的介绍
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. (1). delegate delegate我们常用到的一种声明 Delegate至少0个参 ...