交叉表的sql实现(转)

报表工具做出的交叉表有效的实现了sql中的行列转换。
转帖 wildwave一篇用存储过程实现实现的sql行列转换。原文地址(http://topic.csdn.net/u/20091019/11/67cd55a3-3f42-4db7-a3f8-91dd52a913cd.html)
>>经常遇到发帖求行列转换的代码,用max(decode(..))回复后,十有八九会再问一句:如果列名不固定,或者列数不固定怎么办。就要用存储过程来写,这些存储过程的代码都大同小异,我就想能不能写个通用点的过程
试了一下,把结果发出来
SQL code
 
    
create or replace procedure proc (tabname in varchar2 , col1 in varchar2 , col2 in varchar2 , col3 in varchar2 , viewname in varchar2 default ' v_tmp ' ) as sqlstr varchar2 ( 2000 ): = ' create or replace view ' || viewname || ' as select ' || col1 || ' ' ; c1 sys_refcursor; v1 varchar2 ( 100 ); begin open c1 for ' select distinct to_char( ' || col2 || ' ) from ' || tabname; loop fetch c1 into v1; exit when c1 % notfound; sqlstr: = sqlstr || ' ,max(decode( ' || col2 || ' , ''' || v1 || ''' , ' || col3 || ' ))" ' || v1 || ' " ' ; end loop; close c1; sqlstr: = sqlstr || ' from ' || tabname || ' group by ' || col1; execute immediate sqlstr; end proc ;

这里的几个参数,tabname指的是需要进行行列转换的表名,col1是这个表中行列转换以后要根据哪一列进行分组,那一列的列名。col2传入的是要将行转成列的那一列的列名,col3表示需要进行统计的数据列的列名
viewname传入希望建立的视图的名称,可以不填,默认为v_tmp
这么说很难让人明白..举个例子,引用一个帖子的数据

create table tab (
counter varchar(20), -- 参加考试人数
subject varchar(20), -- 科目
class varchar(20) -- 班级
)


表数据:

counter subject class
36 英语 一班
44 英语 二班
44 数学 二班
33 语文 一班
39 语文 三班


转换后:

一班 二班 三班
英语 36 44 0
数学 0 44 0
语文 33 0 39


编译好过程后,执行
SQL code
 
    
begin proc ( ' tab ' , ' subject ' , ' class ' , ' counter ' ); end ; -- 结果 select * from v_tmp; SUBJECT 一班 三班 二班 数学 44 英语 36 44 语文 33 39

如果对这个结果不是很满意,需要自己进行一些修改,比如空值的地方用0代替,或者需要用别的函数聚合而不是max。可以将过程中的execute immediate那句改成
dbms_output.put_line(sqlstr);
重新编译,执行,输出代码
如果用的是pl/sql dev的sql窗口,到output窗口查看
SQL code
 
    
-- 看到生成的代码 create or replace view v_tmp as select subject , max (decode(class, ' 一班 ' ,counter))"一班" , max (decode(class, ' 三班 ' ,counter))"三班" , max (decode(class, ' 二班 ' ,counter))"二班" from tab group by subject

再加入nvl(),达到修改的目的

如果不想创建这样一个过程,则改成匿名块,需要时运行
SQL code
 
    
declare tabname varchar2 ( 20 ): = ' XXX ' ; -- 'XXX'分别用相应的表名和字段名代替 col1 varchar2 ( 10 ): = ' XXX ' ; col2 varchar2 ( 10 ): = ' XXX ' ; col3 varchar2 ( 10 ): = ' XXX ' ; viewname in varchar2 ( 10 ): = ' v_tmp ' ; sqlstr varchar2 ( 2000 ): = ' create or replace view ' || viewname || ' as select ' || col1 || ' ' ; c1 sys_refcursor; v1 varchar2 ( 100 ); begin open c1 for ' select distinct to_char( ' || col2 || ' ) from ' || tabname; loop fetch c1 into v1; exit when c1 % notfound; sqlstr: = sqlstr || ' ,max(decode( ' || col2 || ' , ''' || v1 || ''' , ' || col3 || ' ))" ' || v1 || ' " ' ; end loop; close c1; sqlstr: = sqlstr || ' from ' || tabname || ' group by ' || col1; -- execute immediate sqlstr; dbms_output.put_line(sqlstr); end ;
完善
代码

1.使用视图
SQL code
 
    
create or replace procedure row_to_col(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 , viewname in varchar2 default ' v_tmp ' ) Authid Current_User as sqlstr varchar2 ( 2000 ): = ' create or replace view ' || viewname || ' as select ' || group_col || ' ' ; c1 sys_refcursor; v1 varchar2 ( 100 ); 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; sqlstr: = sqlstr || ' from ' || tabname || ' group by ' || group_col || case when roworder is not null then ' order by ' || roworder end ; execute immediate sqlstr; end row_to_col;

这里修改了传入参数名,使其更容易理解。继续使用了创建视图这个方法,当然也可以改成用游标传出。
参数:
tabname 需要进行行转列操作的表名;
group_col 查询结果要按某列或某些列分组的字段名;
column_col 要从行转成列的字段;
value_col 需要聚合的值字段;
Aggregate_func 选用的聚合函数,可选,默认为max;
colorder 行转列后列的排序,可选;
roworder 行转列后记录的排序,可选;
when_value_null 若value_col字段的值聚合后为空,则转换成该值,可选;
viewname 创建的视图名称,可选,默认为v_tmp。

举例:
SQL code
 
    
-- 测试数据 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;
我现在想根据year和month分组,将部门转成列。
SQL code
 
    
SQL > select * from rowtocol_test; YEAR MONTH DEPT EXPENDITURE -- -------- ---------- ----- ----------- 2009 1 部门1 50000 2009 2 部门1 20000 2009 2 部门1 30000 2010 1 部门1 35000 2009 2 部门2 40000 2009 3 部门2 25000 2010 2 部门3 60000 2009 2 部门3 15000 2009 2 部门3 10000 9 rows selected SQL > execute row_to_col( ' rowtocol_test ' , ' year,month ' , ' dept ' , ' expenditure ' ); PL / SQL procedure successfully completed SQL > select * from v_tmp; YEAR MONTH 部门1 部门3 部门2 -- -------- ---------- ---------- ---------- ---------- 2009 1 50000 2010 1 35000 2009 3 25000 2009 2 30000 15000 40000 2010 2 60000 SQL >

这个结果可能不是我们想要的,重新调用过程,使用几个可选参数
SQL code
 
    
SQL > execute row_to_col( ' rowtocol_test ' , ' year,month ' , ' dept ' , ' expenditure ' ,Aggregate_func => ' sum ' ,colorder => ' dept ' ,roworder => ' 1,2 ' ,when_value_null => ' 0 ' ); PL / SQL procedure successfully completed SQL > select * from v_tmp; YEAR MONTH 部门1 部门2 部门3 -- -------- ---------- ---------- ---------- ---------- 2009 1 50000 0 0 2009 2 50000 40000 25000 2009 3 0 25000 0 2010 1 35000 0 0 2010 2 0 0 60000 SQL >

进行行转列的也可以是视图
SQL code
 
    
SQL > create view view_rowtocol as select * from rowtocol_test where year = 2009 ; View created SQL > execute row_to_col( ' view_rowtocol ' , ' year,month ' , ' dept ' , ' expenditure ' ,Aggregate_func => ' sum ' ,colorder => ' dept ' ,roworder => ' 1,2 ' ,when_value_null => ' 0 ' ); PL / SQL procedure successfully completed SQL > select * from v_tmp; YEAR MONTH 部门1 部门2 部门3 -- -------- ---------- ---------- ---------- ---------- 2009 1 50000 0 0 2009 2 50000 40000 25000 2009 3 0 25000 0 SQL >

-----------------------------------------------------------
2.稍加修改,使用函数,返回游标。或利用过程里的传出参数
SQL code
 
    
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 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;

在pl/sql dev中可以在sql窗口执行,查看结果
SQL code
 
    
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; ROW_TO_COL_FUNC( ' ROWTOCOL_TEST YEAR MONTH 部门1 部门2 部门3 2009 1 50000 0 0 2009 2 50000 40000 25000 2009 3 0 25000 0 2010 1 35000 0 0 2010 2 0 0 60000

你可能感兴趣的:(交叉表的sql实现(转))