一、行转列pivot

关键函数pivot,其用法如下 pivot(聚合函数 for 列名 in(类型))

select * from table_name pivot(max(column_name)                            --行转列后的列的值value,聚合函数是必须要有的
                               for column_name in(value_1,value_2,value_3)     --需要行转列的列及其对应列的属性1/2/3
                                     )

1、首先举一个简单的例子,创建一个数据表

 create table tmp as select * from (
select '张三' student,'语文' course ,78 score from dual union all
select '张三','数学',87 from dual union all
select '张三','英语',82 from dual union all
select '张三','物理',90 from dual union all
select '李四','语文',65 from dual union all
select '李四','数学',77 from dual union all
select '李四','英语',65 from dual union all
select '李四','物理',85 from dual);

先使用decode或case when方法

 select
student,
max(decode(course, '语文', score)) 语文,
max(decode(course, '数学', score)) 数学,
max(decode(course, '英语', score)) 英语,
max(decode(course, '物理', score)) 物理,
sum(score) total
from tmp
group by student;
-----------------------------------------
select
student,
max(case when course = '语文' then score end) 语文,
max(case when course = '数学' then score end) 数学,
max(case when course = '英语' then score end) 英语,
max(case when course = '物理' then score end) 物理,
sum(score) total
from tmp
group by student;

pivot的使用

 select t.*,
(t.语+t.数+t.外+t.物) as total
from
(select *
from tmp pivot ( max(score) for course in ('语文' as 语 , '数学' as 数, '英语' as 外,'物理' as 物) )
) t;

结果同上

2、实际开发遇到的问题

有一张目标值表,年、月、日的值都是分开多行显示,现需合并成一行显示,具体数据如下:(type:1-->日,2-->月,3-->年;targetvalue:目标值)

select * from MOVEBI.T_GMS_MBI_TARGET_DATA where targetcode = '31227061'

此数据必须先进性处理,要保证数据可以聚合成一条,若直接使用会出现下列情况:

select * from MOVEBI.T_GMS_MBI_TARGET_DATA pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '';

这不是我们想要的结果,具体改进法法如下:

 --方法一:对结果处理
select max(datatime) datatime
,usercode
,deptcode
,deptname
,targetcode
,targetname
,sum(coalesce(day_value,0)) day_value
,sum(coalesce(mon_value,0)) mon_value
,sum(coalesce(year_value,0)) year_value
from(
select datatime,usercode,deptcode,deptname,targetcode,targetname,day_value,mon_value,year_value
from MOVEBI.T_GMS_MBI_TARGET_DATA
pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '')
group by usercode
,deptcode
,deptname
,targetcode
,targetname;
--方法二:对原始表处理
select *
from (select '' datatime,
usercode,
deptcode,
deptname,
targetcode,
targetname,
targetvalue,
type
from MOVEBI.T_GMS_MBI_TARGET_DATA
where datatime in ('', '')
and targetcode = '') t
pivot(max(targetvalue) for type in (1 day_value,2 mon_value,3 year_value)) where targetcode = '';

二、列转行unpivot

根据上面的例子创建tmp_2测试用表

select student,科目,成绩 from tmp_2 unpivot (成绩 for 科目 in (语文, 数学, 英语, 物理));

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

select student,'语文' 科目, (select 语文 from tmp_2 where student=f.student) 成绩 from tmp_2 f
union
select student,'数学' 科目, (select 数学 from tmp_2 where student=f.student) 成绩 from tmp_2 f
union
select student,'英语' 科目, (select 英语 from tmp_2 where student=f.student) 成绩 from tmp_2 f
union
select student,'物理' 科目, (select 物理 from tmp_2 where student=f.student) 成绩 from tmp_2 f
-------------------------------------------
select student,'语文' 科目,语文 from tmp_2
union
select student,'数学' 科目,语文 from tmp_2
union
select student,'英语' 科目,语文 from tmp_2
union
select student,'物理' 科目,语文 from tmp_2

(注:此为学习记录笔记,仅供参考若有问题请指正,后续补充......)

参考文档:https://blog.csdn.net/xiaokui_wingfly/article/details/42419207

参考文档:https://www.cnblogs.com/harvey888/p/6735093.html

参考文档:https://www.cnblogs.com/markfeifei/p/4009343.html

Oracle 行列转换函数pivot、unpivot的使用(二)的更多相关文章

  1. oracle行列转换函数的使用

    oracle 10g wmsys.wm_concat行列转换函数的使用: 首先让我们来看看这个神奇的函数wm_concat(列名),该函数可以把列值以","号分隔起来,并显示成一行 ...

  2. Oracle11g 行列转换函数PIVOT and UNPIVOT

    作为Oracle开发工程师,推荐大伙看看 PIVOT and UNPIVOT Operators in Oracle Database 11g Release 1 This article shows ...

  3. oracle 行列转换函数之WM_CONCAT和LISTAGG的使用(一)

    一.wm_concat函数 wm_concat能够实现同样的功能,但是有时在11g中使用需要用to_char()进行转换,否则会出现不兼容现象(WMSYS.WM_CONCAT: 依赖WMSYS 用户, ...

  4. Oracle行列转换

    一.建表与插入数据 1.1.建表 create table kecheng ( id NUMBER, name ), course ), score NUMBER ); insert into kec ...

  5. oracle 行列转换

    oracle 行列转换列名如果是数字,用双引号包住  如下: --  建表 create table workinfo(wid integer primary key,sid integer ,CON ...

  6. Oracle 大小写转换函数

    Oracle 大小写转换函数 转大写UPPER 转小写LOWER 测试: select UPPER('Test') as u from dual; select LOWER('Test') as l ...

  7. SQL(横表和纵表)行列转换,PIVOT与UNPIVOT的区别和使用方法举例,合并列的例子

    使用过SQL Server 2000的人都知道,要想实现行列转换,必须综合利用聚合函数和动态SQL,具体实现起来需要一定的技巧,而在SQL Server 2005中,使用新引进的关键字PIVOT/UN ...

  8. 行列转换小结 Pivot ,Unpivot (转,改)

    行专列 Pivot 1)SQL 2000版本 静态 SELECT ID , SUM(CASE Code WHEN 'Item1' THEN Value END) AS Item1 , SUM(CASE ...

  9. KingbaseES 行列转换函数

    关键字:    行专列,列转行, pivot, unpivot 行列转换是在数据分析中经常用到的一项功能,KingbaseES从V8R6C3B0071版本开始通过扩展插件(kdb_utils_func ...

随机推荐

  1. 改善C#公共程序类库质量的10种方法(转)

    出处:http://www.cnblogs.com/JamesLi2015/p/3140897.html 最近重构一套代码,运用以下几种方法,供参考. 1  公共方法尽可能的使用缓存 public s ...

  2. 编写高质量代码改善C#程序的157个建议——建议125:避免用FCL的类型名称命名自己的类型

    建议125:避免用FCL的类型名称命名自己的类型 试想过自己写一个Socket类型吗?如果没有,我们来尝试一下: public class Socket { //省略 } 把以上代码同某些其他工具类封 ...

  3. 编写高质量代码改善C#程序的157个建议——建议103:区分组合和继承的应用场合

    建议103:区分组合和继承的应用场合 继承所带来的多态性虽然是面向对象的一个重要特性,但这种特性不能在所有的场合中滥用.继承应该被当做设计架构的有用补充,而不是全部. 组合不能用于多态,但组合使用的频 ...

  4. C#函数式程序设计之泛型(上)

    在面向对象语言中,我们可以编写一个元素为某个专用类型(可能需要为此创建一个ListElement)的List类,或者使用一个非常通用.允许添加任何类型元素的基类(在.NET中,首先想到的是System ...

  5. 用JAVA实现无等待数据库连接池

    我们都知道数据库连接是一种有限和非常昂贵的应用资源,怎样对这些资源进行高效的管理,能有效的改善整个系统的性能和健壮性.数据库连接池正是针对这个问题而提出来的. 数据库连接负责分配.释放和管理数据库连接 ...

  6. Java多线程设计模式(二)

        目录(?)[-] Guarded Suspension Pattern Balking Pattern Producer-Consumer Pattern   Guarded Suspensi ...

  7. Android-读取操作系统通话记录并/拨打电话/发送短信/复制号码到拨号盘

    apps目录的contacts应用(有读取通话记录功能),是访问provider目录的provider.contacts应用(有暴露通话记录),所以要阅读Android操作系统源码-->pack ...

  8. 前台通过form表单向Django后台传输数据,Django处理后返回给前台

    摘要:Django前后台数据传递 通过action将数据传输给apitest这个地址,使用get方法传递,此处需要传递name="request_method"的下拉列表值和nam ...

  9. ASP.NET MVC 如何使用自定义过滤器(筛选器)

    继承*****Attribute(筛选器三种具体类)-->重写方法-->标记在控制器 或者 方法上面 或者 在FilterConfig中Add [类名(类属性 = 值)]还有[AllowA ...

  10. Android 开发权限设置中英对照说明详解

    android.permission.ACCESS_CHECKIN_PROPERTIES 允许读写访问 "properties"表在checkin数据库中,改值可以修改上传( Al ...