--测试数据
create table rowtocol_test as
select 2009 year,1 month,'部门1' dept,50000 expenditure from dual
union all select 2009,2,'部门1',20000 from dual
union all select 2009,2,'部门1',30000 from dual
union all select 2010,1,'部门1',35000 from dual
union all select 2009,2,'部门2',40000 from dual
union all select 2009,3,'部门2',25000 from dual
union all select 2010,2,'部门3',60000 from dual
union all select 2009,2,'部门3',15000 from dual
union all select 2009,2,'部门3',10000 from dual;

--建存储过程 返回游标
create or replace function row_to_col_func(tabname in varchar2,
group_col in varchar2,
column_col in varchar2,
value_col in varchar2,
Aggregate_func in varchar2 default 'max',
colorder in varchar2 default null,
roworder in varchar2 default null,
when_value_null in varchar2 default null
)return sys_refcursor
Authid Current_User
as
/*
参数:
tabname 需要进行行转列操作的表名;
group_col 查询结果要按某列或某些列分组的字段名;
column_col 要从行转成列的字段;
value_col 需要聚合的值字段;
Aggregate_func 选用的聚合函数,可选,默认为max;
colorder 行转列后列的排序,可选;
roworder 行转列后记录的排序,可选;
when_value_null 若value_col字段的值聚合后为空,则转换成该值,可选;
--viewname 创建的视图名称,可选,默认为v_tmp。
*/
sqlstr varchar2(2000):='select '||group_col||' ';
c1 sys_refcursor;
v1 varchar2(100);
cur sys_refcursor;
begin
open c1 for 'select distinct '||column_col||' from '||tabname||case when colorder is not null then ' order by '||colorder end;
loop
fetch c1 into v1;
exit when c1%notfound;
sqlstr:=sqlstr||chr(10)||','||case when when_value_null is not null then 'nvl(' end||
Aggregate_func||'(decode(to_char('||column_col||'),'''||v1||''','||value_col||'))'||
case when when_value_null is not null then chr(44) ||when_value_null||chr(41) end||'"'||v1||'"';
end loop;
close c1;
open cur for sqlstr||' from '||tabname||' group by '||group_col||case when roworder is not null then ' order by '||roworder end;
return cur;
end row_to_col_func;

--
select row_to_col_func('rowtocol_test','year,month','dept','expenditure',Aggregate_func => 'sum',colorder => 'dept',roworder => '1,2',when_value_null => '0')
from dual;

转自:http://bbs.csdn.net/topics/330039676

ORACLE 行转列的通用过程的更多相关文章

  1. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  2. Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

    这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...

  3. ORACLE行转列通用过程(转)

    1.使用视图 SQL code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 create or r ...

  4. ORACLE行转列通用过程

    create or replace procedure row_to_col(tabname in varchar2,                                   group_ ...

  5. Oracle 行转列 动态出转换的列

    本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ...

  6. oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...

  7. Oracle行转列LISTAGG函数

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

  8. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  9. Oracle行转列、列转行的Sql语句总结(转)

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...

随机推荐

  1. 四-3、Mirror(镜像)、Spin(旋转)、创建组

    1.Mirror 1.针对单个器件 2.针对整个模块 2.Spin(旋转)(平时用的比较少) 3.创建组(经常用到)(主要是在布局时,发现后期需要移动整个模组)(创建为group) 1.创建组---- ...

  2. 20180813-Java 重写(Override)与重载(Overload)

    Java 重写(Override)与重载(Overload) class Animal{ public void move(){ System.out.println("动物可以移动&quo ...

  3. django缓存优化(三)

    This should give you a feel for how this module operates:: import memcache mc = memcache.Client(['12 ...

  4. 影视感悟专题---1、B站-魔兽世界代理及其它乱七八糟

    影视感悟专题---1.B站-魔兽世界代理及其它乱七八糟 一.总结 一句话总结: 看过的东西都可以学下,这样既可以学习那些东西,都是对自己生活学习有帮助的,还可以弥补自己每天学的东西的不够 1.< ...

  5. php面试专题---15、MySQL数据库基础考察点

    php面试专题---15.MySQL数据库基础考察点 一.总结 一句话总结: 注意:只写精品 1.mysql定义int(3),那么我存1234就错了么? 不是:无影响:只会影响显示字符的个数:可以为整 ...

  6. 622FThe Sum of the k-th Powers

    题目大意 求$\sum_{i=1}^{n} i^k$ 分析 我们发现这是一个$k+1$次多项式 因此我们求出前$k+2$项然后插值即可 由于$x_i = i$ 因此公式里面的乘机可以通过预处理然后循环 ...

  7. 新手如何创建一个vue项目 ---vue---新手创建第一个项目

    1.第一步安装node.js https://nodejs.org/en/ 前期可以下载软件包,后期熟练以后可以通过nvm进行 Node的版本切换以及升级 然后window+r  输入cmd 打开命令 ...

  8. Eclipse报内存溢出

    (1)在配置tomcat的JDK里面设置.Window-->proference->Myeclipse-->servers-->Tomcat5-->JDK里面设置: -X ...

  9. Nginx证书配置:tomcat证书jks文件转nginx证书.cet和key文件

    Nginx证书配置:tomcat证书jks文件转nginx证书.cet和key文件1.查看jks文件中的entry. keytool -list -keystore server.jks Enter ...

  10. 第 2 章 前端基础之CSS

    一.CSS语法 CSS规则由两个主要的部分构成:选择器,以及一条或多条声明. ''' selector { property: value; property: value; ... property ...