原始数据:

方式一:

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. SSD 简介—— NAND 芯片介绍

    制作 存储芯片的制作和其他芯片制作大致相同,从沙子中提取单晶硅制作晶圆再封装芯片. 闪存芯片从架构上分为NOR和NAND NOR Flash的source line把每个cell都并联起来,而NAND ...

  2. Oracle字符串行专列(字符串聚合技术)

    原文链接:http://oracle-base.com/articles/misc/string-aggregation-techniques.php 1     String Aggregation ...

  3. 《系列二》-- 3、FactoryBean 的使用

    目录 FactoryBean 解决的问题 FactoryBean 接口初识 改造结果 最后的补充 回顾下 FactoryBean 的应用 factory-method 和 factory-bean 的 ...

  4. 项目实战:Qt+iMax6生命探测仪(探测障碍物、静止目标、动态目标、生命目标、探测半径、探测前方雷达显示、动态目标轨迹显示、探测热力图、探测过程存储与回放)

    若该文为原创文章,转载请注明原文出处本文章博客地址:https://blog.csdn.net/qq21497936/article/details/110994486长期持续带来更多项目与技术分享, ...

  5. go-ini解析ini文件

    文档 https://github.com/go-ini/ini https://ini.unknwon.io/docs/intro/getting_started go get -u gopkg.i ...

  6. 【Azure 应用服务】Azure Function 中运行Powershell 脚本,定位 -DefaultProfile 引发的错误

    问题描述 突然之间,使用PowerShell脚本 Get-AzVirtualNetwork 获取虚拟网络信息时,如果带上  -DefaultProfile $sub 参数,就出现 Azure cred ...

  7. gopkg.in/go-playground/validator中比较有用的标签

    -  忽略|  或omitempty 有则验证,空值则不验证dive  潜入到切片.数组.映射中,例如 NumList []int `validate:"len=2,dive,gt=18&q ...

  8. Glide源码解析三(注册组件)

    转载请标明出处,维权必究: https://www.cnblogs.com/tangZH/p/12900387.html Glide源码解析一,初始化 Glide源码解析二-into方法 Glide源 ...

  9. Java与sql中的字符串表示

    在 Java 中,双引号 "" 用于表示字符串字面量,而单引号 '' 用于表示字符字面量.这意味着在 Java 中,您可以使用双引号来包围包含任意数量字符的字符串,包括零个字符(空 ...

  10. Dubbo基本见解

    1.dubbo主要角色 Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. a.订阅注册中心,注册中心广播服务变更,第一次会主动全量pull所有信息,后面增量会 ...