oracle行转列过程,ORACLE 行转列的通用过程

--测试数据

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

原文:http://www.cnblogs.com/kenwong/p/3976643.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值