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 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?
1
2
3
4
5
6
7
8
9
10
11
--测试数据
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?
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
28
29
30
31
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
  
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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?
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
28
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
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
<Cursor>
 
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

原文:http://bbs.csdn.net/topics/330039676

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

  1. ORACLE行转列通用过程

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

  2. oracle 行转列 分析函数

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

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

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

  4. ORACLE 行转列的通用过程

    --测试数据create table rowtocol_test asselect 2009 year,1 month,'部门1' dept,50000 expenditure from dualun ...

  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. N1游记

    考试一年前:要认真学文化课,所以还是别报七月的了吧,等到年底就该稳了. 考试半年前:虽然暑假没学,但是到了年底就该稳了. 考试一个月前:我咋还要考N1,算了不管了,到时候再说吧. 考试一周前:我一定要 ...

  2. 数据结构与算法(2)----->字符串

    1. 字符串的一些特点 1.1  广泛性 (1)字符串可以看作是字符类型的数组----->所以可能会涉及排序+查找; (2)很多问题都可以转化为字符串类型的方法去解决; 需要注意的是:用java ...

  3. poj1325机器工作——二分图最小点覆盖

    题目:http://poj.org/problem?id=1325 二分图求最大匹配,即为最小点覆盖: 一开始我写得较麻烦,求出最大匹配又去搜增广路,打标记求最小点覆盖: 然而两种方法都没写“ans= ...

  4. MongoDB主从复制,主主复制

    MongoDB主从复制,是不需要像mysql那样从数据库事先要完整的主数据快照背景介绍:mongodb支持一主一从或多从复制1)    master节点: mongod --dbpath=/usr/M ...

  5. POJ3259(ford判环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 39078   Accepted: 14369 Descr ...

  6. Oracle字段增删改方法总结

    一.修改字段的语法:alter table tablename modify (字段名 类型 [default value][null/not null],….);有一个表名为tb,字段段名为name ...

  7. session.write类型引发的思考---Mina Session.write流程探索.doc--zhengli

    基于Mina开发网络通信程序,在传感器数据接入领域应用的很广泛,今天我无意中发现一个问题,那就是我在前端session.write(msg)数据出去之后,却没有经过Filter的Encoder方法,同 ...

  8. unreal Script(US)一些注意事项

    转自:http://www.cnblogs.com/dongbo/archive/2012/07/10/2585311.html 1.如果计算量很大,考虑使用native Code来完成. 2.如果代 ...

  9. Python文件运行时报TabError: inconsistent use of tabs and spaces in indentation

    1. 问题描述 Python文件运行时报TabError: inconsistent use of tabs and spaces in indentation 2. 问题原因 tab 和 space ...

  10. 《Java多线程编程核心技术》读后感(十二)

    类ThreadLocal的使用 主要解决的是每个线程绑定自己的值,可以将ThreadLocal类比喻成全局存放数据的盒子,盒子中可以存储每个线程私有数据. 类ThreadLocal解决的是变量在不同线 ...