Oracle 几种行转列的方式 sum+decode sum+case when pivot
原始数据:

方式一:
select t_name,
sum(decode(t_item, 'item1', t_num, 0)) item1,
sum(decode(t_item, 'item2', t_num, 0)) item2,
sum(decode(t_item, 'item3', t_num, 0)) item3,
sum(t_num) total
from test
group by t_name;

方式二:
select t_name,
sum(case
when t_item = 'item1' then
t_num
else
0
end) item1,
sum(case
when t_item = 'item2' then
t_num
else
0
end) item2,
sum(case
when t_item = 'item3' then
t_num
else
0
end) item3,
sum(t_num) total
from test
group by t_name;

方式三:
select t.*, (nvl(t.item1, 0) + nvl(t.item2, 0) + nvl(t.item3, 0)) as total
from (select *
from test pivot(sum(t_num) for t_item in('item1' as item1,
'item2' as item2,
'item3' as item3))) t;

unpivot的使用:
select t_name, t_item, t_num
from (
select *
from test pivot(sum(t_num) for t_item in('item1' as item1,
'item2' as item2,
'item3' as item3))
) unpivot(t_num for t_item in(item1,item2,item3));

Oracle 几种行转列的方式 sum+decode sum+case when pivot的更多相关文章
- oracle中的行转列,列转行
行转列:源表: 方法1:case when select y,sum(case when q=1 then amt end) q1,sum(case when q=2 then amt end) q2 ...
- oracle sql 字段行转列
数据库中原先如图: 现在要吧WHMM行转列: conncect by用于确定列的个数
- oracle 存储过程-动态行转列,解决。
包头 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure Alarm_ContentsByTi ...
- oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决
--Type CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT ( CURR_STR clob, STATIC FUN ...
- oracle输出多行多列数据
--方法一 匿名块中直接 dbms_output输出declare v_sql varchar2(200); v_cursor sys_refcursor; type v_type is ...
- Oracle中的行转列例子详解
--场景1: A B a a a b b 希望实现如下效果: a ,, b , create table tmp as B from dual union all B from dual union ...
- js 创建Table,每行3列的方式
table: <table style="width:100%" id="appDatas"></table> 1. var table ...
- Oracle行转列LISTAGG函数
工作过程中需要将查询的数据分组并显示在一行.以往的工作经验,在sql server中可以用for xml path来实现. 现提供Oracle数据库的行转列方式 oracle11g官方文档简介如下: ...
- oracle行转列和列转行(pivot 和 unpivot 函数,wm_concat函数 )
create table demo(id int,name varchar(20),nums int); ---- 创建表insert into demo values(1, '苹果', 1000); ...
- Oracle学习笔记:一个简单的行转列例子
一个简单的行列转换例子,原始数据. create table temp_cwh_student ( name ), subject ), score ) ) select * from temp_cw ...
随机推荐
- Vue中数组变动监听
Vue中数组变动监听 Vue的通过数据劫持的方式实现数据的双向绑定,即使用Object.defineProperty()来实现对属性的劫持,但是Object.defineProperty()中的set ...
- Shiro实战1-介绍
什么是 Shiro 官网:http://shiro.apache.org/ shiro是一款主流的 Java 安全框架,不依赖任何容器,可以运行在 Java SE和 Java EE 项目中,它的主要作 ...
- UTF-8 的理解
举个简单的例子: Unicode 只是一个业界标准,具体一个字符占多少字节,取决于编码方式,包括 UTF-8 UTF-16 GB2312 等 "汉" 在 UTF-8 中占到 3 个 ...
- nosql几种热门数据库的优缺点及应用场景
MongoDB.ElasticSearch.Redis.HBase这四种热门数据库的优缺点及应用场景 https://www.cnblogs.com/chong-zuo3322/p/12869059. ...
- Linux驱动开发笔记(四):设备驱动介绍、熟悉杂项设备驱动和ubuntu开发杂项设备Demo
前言 驱动的开发需要先熟悉基本概念类型,本篇讲解linux杂项设备基础,还是基于虚拟机ubuntu去制作驱动,只需要虚拟机就可以尝试编写注册杂项设备的基本流程. linux三大设备驱动 字符设 ...
- django学习第十一天---django操作cookie和session
Cookie cookie解析 会话 http协议是无状态的,无连接的 导致每次客户端访问服务端需要登录成功之后才能访问的页面,都需要用户再重新登录一遍,用户体验极差. 客户端想了个办法,cookie ...
- 【Azure Key Vault】客户端获取Key Vault机密信息全部失败问题分析
问题描述 在应用中获取存储在Azure Key Vault的机密信息,全部失败. 报错日志内容如下: [reactor-http-epoll-4] [reactor.netty.http.client ...
- 【Azure 环境】Azure Key Vault 采用自签名证书,是否需要CA provider
关于 Azure Key Vault 证书,密钥保管库证书支持适用于 x509 证书管理,它提供以下行为: 允许证书所有者通过密钥保管库创建过程或通过导入现有证书来创建证书. 包括自签名证书和证书颁发 ...
- 【Azure 环境】当在Azure 环境中调用外部接口不通时,如何定位SSL Certificate Problem
问题描述 如果在Azure VM中,发现同一个API,一台VM可以访问成功,另外一台访问失败.如何来调试并定位问题呢? 问题分析 第一步,查看访问外部API不通时候出现什么错误.如果没有明确的错误消息 ...
- 解决centos启动zookeeper集群,但是状态显示报错:Error contacting service. It is probably not running 与连接超时,连接被拒绝问题
安装zookeeper-3.4.10的时候,启动正常没报错,但bin/zkServer.sh status查看状态的时候却出现错误,如下: 这些都是我之前出现的问题,刚开始我出现的问题是连接超时,后来 ...