原始数据:

方式一:

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的更多相关文章

  1. oracle中的行转列,列转行

    行转列:源表: 方法1:case when select y,sum(case when q=1 then amt end) q1,sum(case when q=2 then amt end) q2 ...

  2. oracle sql 字段行转列

    数据库中原先如图: 现在要吧WHMM行转列: conncect by用于确定列的个数

  3. oracle 存储过程-动态行转列,解决。

    包头 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure Alarm_ContentsByTi ...

  4. oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决

    --Type CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT ( CURR_STR clob, STATIC FUN ...

  5. oracle输出多行多列数据

    --方法一  匿名块中直接 dbms_output输出declare  v_sql    varchar2(200);  v_cursor sys_refcursor;  type v_type is ...

  6. Oracle中的行转列例子详解

    --场景1: A B a a a b b 希望实现如下效果: a ,, b , create table tmp as B from dual union all B from dual union ...

  7. js 创建Table,每行3列的方式

    table: <table style="width:100%" id="appDatas"></table> 1. var table ...

  8. Oracle行转列LISTAGG函数

    工作过程中需要将查询的数据分组并显示在一行.以往的工作经验,在sql server中可以用for xml path来实现. 现提供Oracle数据库的行转列方式 oracle11g官方文档简介如下: ...

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

    create table demo(id int,name varchar(20),nums int); ---- 创建表insert into demo values(1, '苹果', 1000); ...

  10. Oracle学习笔记:一个简单的行转列例子

    一个简单的行列转换例子,原始数据. create table temp_cwh_student ( name ), subject ), score ) ) select * from temp_cw ...

随机推荐

  1. MySQL基础之DCL语句

    DCL(Data Control Language)语句:数据控制语句. 用途:控制数据库.表.字段.用户的访问权限和安全级别. 常用关键字:grant.revoke等 一般用于管理数据库和用户的权限 ...

  2. Spring事务使用注意事项

    Spring提供的事务使用起来很方便,一个@Transactional注解就搞定全部,但是如果不注意,也会踩坑 提到事务就应该想到至少以下几点: 1.在事务方法中加锁,可能会导致锁失效 无论是Java ...

  3. Vue+SpringBoot+ElementUI实战学生管理系统-5.用户管理模块

    1.章节介绍 前一篇介绍了项目的API接口设计,这一篇编写用户管理模块,需要的朋友可以拿去自己定制.:) 2.获取源码 源码是捐赠方式获取,详细请QQ联系我 :)! 3.项目截图 列表操作 动态图 4 ...

  4. golang在 ubuntu下交叉编译报错 gcc: error: unrecognized command line option ‘-mthreads’; did you mean ‘-pthread’?

    前置说明: 之前一直都是用centos 7做开发机, 因为工作需要用c2 工具sliver编译木马而依赖 mingw64,但是centos安装这个非常麻烦, 就换了ubuntu开发机; 现需要交叉编译 ...

  5. go语言编程常见问题

    在Goland中运行单元测试报错Error: Cannot find package 如下图,在Goland中运行单元测试时报错:"Error: Cannot find package&qu ...

  6. 在vue项目中使用scss语法的准备步骤

    在vue项目中使用scss语法的准备步骤 个人总结: 在项目根目录cmd控制台中使用以下命令行,安装vue项目中使用scss的相关依赖; 在["项目根目录/build/webpack.bas ...

  7. Qt+MPlayer音乐播放器开发笔记(二):交叉编译MPlayer以及部署到开发板播放演示

    前言   在ubuntu上arm交叉编译MPlayer播放器,并部署到开发板播放音乐.   Demo                Mplayer   MPlayer是一款开源多媒体播放器,以GNU通 ...

  8. ASP.NET Core 微信支付(三)【查询订单 APIV3】

    官方参考资料 查询订单 理论实战 官方提供两种查询订单的方式,一种是根据商户自己生成的订单号查询,一种方式是根据微信生成的订单号查询.而我使用的就是第一种方式. 调用微信支付的查询订单,需要生成签名, ...

  9. 【LeetCode链表#6】移除链表元素

    移除链表元素 题目 力扣题目链接(opens new window) 题意:删除链表中等于给定值 val 的所有节点. 示例 1: 输入:head = [1,2,6,3,4,5,6], val = 6 ...

  10. Java -----多线程 创建线程的方式三: 实现Callable接口----JDK 5.0 新增

    1 package bytezero.thread2; 2 3 import java.util.concurrent.Callable; 4 import java.util.concurrent. ...