oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了

众所周知,静态SQL的输出结构必须也是静态的。对于经典的行转列问题,如果行数不定导致输出的列数不定,标准的答案就是使用动态SQL, 到11G里面则有XML结果的PIVOT。

但是 oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了。上代码 直接点。

CREATE OR REPLACE
type PivotImpl_shx as object
(
ret_type anytype, -- The return type of the table function
stmt varchar2(32767),
fmt varchar2(32767),
cur integer,
static function ODCITableDescribe( rtype out anytype, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number := 0 )
return number,
static function ODCITablePrepare( sctx out PivotImpl_shx, ti in sys.ODCITabFuncInfo, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number := 0 )
return number,
static function ODCITableStart( sctx in out PivotImpl_shx, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number := 0 )
return number,
member function ODCITableFetch( self in out PivotImpl_shx, nrows in number, outset out anydataset )
return number,
member function ODCITableClose( self in PivotImpl_shx )
return number
)
/

create or replace type body PivotImpl_shx as
static function ODCITableDescribe( rtype out anytype, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number )
return number
is
atyp anytype;
cur integer;
numcols number;
desc_tab dbms_sql.desc_tab2;
rc sys_refcursor;
t_c2 varchar2(32767);
t_fmt varchar2(1000);
begin
cur := dbms_sql.open_cursor;
dbms_sql.parse( cur, p_stmt, dbms_sql.native );
dbms_sql.describe_columns2( cur, numcols, desc_tab );
dbms_sql.close_cursor( cur );
--
anytype.begincreate( dbms_types.typecode_object, atyp );
for i in 1 .. numcols - 2
loop
atyp.addattr( desc_tab( i ).col_name
, case desc_tab( i ).col_type
when 1 then dbms_types.typecode_varchar2
when 2 then dbms_types.typecode_number
when 9 then dbms_types.typecode_varchar2
when 11 then dbms_types.typecode_varchar2 -- show rowid as varchar2
when 12 then dbms_types.typecode_date
when 208 then dbms_types.typecode_varchar2 -- show urowid as varchar2
when 96 then dbms_types.typecode_char
when 180 then dbms_types.typecode_timestamp
when 181 then dbms_types.typecode_timestamp_tz
when 231 then dbms_types.typecode_timestamp_ltz
when 182 then dbms_types.typecode_interval_ym
when 183 then dbms_types.typecode_interval_ds
end
, desc_tab( i ).col_precision
, desc_tab( i ).col_scale
, case desc_tab( i ).col_type
when 11 then 18 -- for rowid col_max_len = 16, and 18 characters are shown
else desc_tab( i ).col_max_len
end
, desc_tab( i ).col_charsetid
, desc_tab( i ).col_charsetform
);
end loop;
if instr( p_fmt, ‘@[email protected]‘ ) > 0
then
t_fmt := p_fmt;
else
t_fmt := ‘@[email protected]‘;
end if;
open rc for replace( ‘select distinct ‘ || t_fmt || ‘
from( ‘ || p_stmt || ‘ )
order by ‘ || t_fmt
, ‘@[email protected]‘
, desc_tab( numcols - 1 ).col_name
);
loop
fetch rc into t_c2;
exit when rc%notfound;
atyp.addattr( t_c2
, case desc_tab( numcols ).col_type
when 1 then dbms_types.typecode_varchar2
when 2 then dbms_types.typecode_number
when 9 then dbms_types.typecode_varchar2
when 11 then dbms_types.typecode_varchar2 -- show rowid as varchar2
when 12 then dbms_types.typecode_date
when 208 then dbms_types.typecode_urowid
when 96 then dbms_types.typecode_char
when 180 then dbms_types.typecode_timestamp
when 181 then dbms_types.typecode_timestamp_tz
when 231 then dbms_types.typecode_timestamp_ltz
when 182 then dbms_types.typecode_interval_ym
when 183 then dbms_types.typecode_interval_ds
end
, desc_tab( numcols ).col_precision
, desc_tab( numcols ).col_scale
, case desc_tab( numcols ).col_type
when 11 then 18 -- for rowid col_max_len = 16, and 18 characters are shown
else desc_tab( numcols ).col_max_len
end
, desc_tab( numcols ).col_charsetid
, desc_tab( numcols ).col_charsetform
);
end loop;
close rc;
atyp.endcreate;
anytype.begincreate( dbms_types.typecode_table, rtype );
rtype.SetInfo( null, null, null, null, null, atyp, dbms_types.typecode_object, 0 );
rtype.endcreate();
return odciconst.success;
exception
when others then
return odciconst.error;
end;
--
static function ODCITablePrepare( sctx out PivotImpl_shx, ti in sys.ODCITabFuncInfo, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number )
return number
is
prec pls_integer;
scale pls_integer;
len pls_integer;
csid pls_integer;
csfrm pls_integer;
elem_typ anytype;
aname varchar2(30);
tc pls_integer;
begin
tc := ti.RetType.GetAttrElemInfo( 1, prec, scale, len, csid, csfrm, elem_typ, aname );
--
if instr( p_fmt, ‘@[email protected]‘ ) > 0
then
sctx := PivotImpl_shx( elem_typ, p_stmt, p_fmt, null );
else
sctx := PivotImpl_shx( elem_typ, p_stmt, ‘@[email protected]‘, null );
end if;
return odciconst.success;
end;
--
static function ODCITableStart( sctx in out PivotImpl_shx, p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number )
return number
is
cur integer;
numcols number;
desc_tab dbms_sql.desc_tab2;
t_stmt varchar2(32767);
type_code pls_integer;
prec pls_integer;
scale pls_integer;
len pls_integer;
csid pls_integer;
csfrm pls_integer;
schema_name varchar2(30);
type_name varchar2(30);
version varchar2(30);
attr_count pls_integer;
attr_type anytype;
attr_name varchar2(100);
dummy2 integer;
begin
cur := dbms_sql.open_cursor;
dbms_sql.parse( cur, p_stmt, dbms_sql.native );
dbms_sql.describe_columns2( cur, numcols, desc_tab );
dbms_sql.close_cursor( cur );
--
for i in 1 .. numcols - 2
loop
t_stmt := t_stmt || ‘, "‘ || desc_tab( i ).col_name || ‘"‘;
end loop;
--
type_code := sctx.ret_type.getinfo( prec
, scale
, len
, csid
, csfrm
, schema_name
, type_name
, version
, attr_count
);
for i in numcols - 1 .. attr_count
loop
type_code := sctx.ret_type.getattreleminfo( i
, prec
, scale
, len
, csid
, csfrm
, attr_type
, attr_name
);
t_stmt := t_stmt || replace( ‘, max( decode( ‘ || sctx.fmt || ‘, ‘‘‘ || attr_name || ‘‘‘, ‘ || desc_tab( numcols ).col_name || ‘ ) )‘
, ‘@[email protected]‘
, desc_tab( numcols - 1 ).col_name
);
end loop;
t_stmt := ‘select ‘ || substr( t_stmt, 2 ) || ‘ from ( ‘ || sctx.stmt || ‘ )‘;
for i in 1 .. numcols - 2
loop
if i = 1
then
t_stmt := t_stmt || ‘ group by "‘ || desc_tab( i ).col_name || ‘"‘;
else
t_stmt := t_stmt || ‘, "‘ || desc_tab( i ).col_name || ‘"‘;
end if;
end loop;
--
--dbms_output.put_line( t_stmt );
sctx.cur := dbms_sql.open_cursor;
dbms_sql.parse( sctx.cur, t_stmt, dbms_sql.native );
for i in 1 .. attr_count
loop
type_code := sctx.ret_type.getattreleminfo( i
, prec
, scale
, len
, csid
, csfrm
, attr_type
, attr_name
);
case type_code
when dbms_types.typecode_char then dbms_sql.define_column( sctx.cur, i, ‘x‘, 32767 );
when dbms_types.typecode_varchar2 then dbms_sql.define_column( sctx.cur, i, ‘x‘, 32767 );
when dbms_types.typecode_number then dbms_sql.define_column( sctx.cur, i, cast( null as number ) );
when dbms_types.typecode_date then dbms_sql.define_column( sctx.cur, i, cast( null as date ) );
when dbms_types.typecode_urowid then dbms_sql.define_column( sctx.cur, i, cast( null as urowid ) );
when dbms_types.typecode_timestamp then dbms_sql.define_column( sctx.cur, i, cast( null as timestamp ) );
when dbms_types.typecode_timestamp_tz then dbms_sql.define_column( sctx.cur, i, cast( null as timestamp with time zone ) );
when dbms_types.typecode_timestamp_ltz then dbms_sql.define_column( sctx.cur, i, cast( null as timestamp with local time zone ) );
when dbms_types.typecode_interval_ym then dbms_sql.define_column( sctx.cur, i, cast( null as interval year to month ) );
when dbms_types.typecode_interval_ds then dbms_sql.define_column( sctx.cur, i, cast( null as interval day to second ) );
end case;
end loop;
dummy2 := dbms_sql.execute( sctx.cur );
return odciconst.success;
end;
--
member function ODCITableFetch( self in out PivotImpl_shx, nrows in number, outset out anydataset )
return number
is
c1_col_type pls_integer;
type_code pls_integer;
prec pls_integer;
scale pls_integer;
len pls_integer;
csid pls_integer;
csfrm pls_integer;
schema_name varchar2(30);
type_name varchar2(30);
version varchar2(30);
attr_count pls_integer;
attr_type anytype;
attr_name varchar2(100);
v1 varchar2(32767);
n1 number;
d1 date;
ur1 urowid;
ids1 interval day to second;
iym1 interval year to month;
ts1 timestamp;
tstz1 timestamp with time zone;
tsltz1 timestamp with local time zone;
begin
outset := null;
if nrows < 1
then
-- is this possible???
return odciconst.success;
end if;
--
--dbms_output.put_line( ‘fetch‘ );
if dbms_sql.fetch_rows( self.cur ) = 0
then
return odciconst.success;
end if;
--
--dbms_output.put_line( ‘done‘ );
type_code := self.ret_type.getinfo( prec
, scale
, len
, csid
, csfrm
, schema_name
, type_name
, version
, attr_count
);
anydataset.begincreate( dbms_types.typecode_object, self.ret_type, outset );
outset.addinstance;
outset.piecewise();
for i in 1 .. attr_count
loop
type_code := self.ret_type.getattreleminfo( i
, prec
, scale
, len
, csid
, csfrm
, attr_type
, attr_name
);
--dbms_output.put_line( attr_name );
case type_code
when dbms_types.typecode_char then
dbms_sql.column_value( self.cur, i, v1 );
outset.setchar( v1 );
when dbms_types.typecode_varchar2 then
dbms_sql.column_value( self.cur, i, v1 );
outset.setvarchar2( v1 );
when dbms_types.typecode_number then
dbms_sql.column_value( self.cur, i, n1 );
outset.setnumber( n1 );
when dbms_types.typecode_date then
dbms_sql.column_value( self.cur, i, d1 );
outset.setdate( d1 );
when dbms_types.typecode_urowid then
dbms_sql.column_value( self.cur, i, ur1 );
outset.seturowid( ur1 );
when dbms_types.typecode_interval_ds then
dbms_sql.column_value( self.cur, i, ids1 );

outset.setintervalds( ids1 );
when dbms_types.typecode_interval_ym then
dbms_sql.column_value( self.cur, i, iym1 );
outset.setintervalym( iym1 );
when dbms_types.typecode_timestamp then
dbms_sql.column_value( self.cur, i, ts1 );
outset.settimestamp( ts1 );
when dbms_types.typecode_timestamp_tz then
dbms_sql.column_value( self.cur, i, tstz1 );
outset.settimestamptz( tstz1 );
when dbms_types.typecode_timestamp_ltz then
dbms_sql.column_value( self.cur, i, tsltz1 );
outset.settimestampltz( tsltz1 );
end case;
end loop;
outset.endcreate;
return odciconst.success;
end;
--
member function ODCITableClose( self in PivotImpl_shx )
return number
is
c integer;
begin
c := self.cur;
dbms_sql.close_cursor( c );
return odciconst.success;
end;
end;
/

-- 在外面包装一层PLSQL函数:
create or replace
function pivot_shx( p_stmt in varchar2, p_fmt in varchar2 := ‘upper(@[email protected])‘, dummy in number := 0 )
return anydataset pipelined using PivotImpl_shx;
/

测试例子

with tmp_tab as(
select da.duty_user ucode, u.user_name as uname,
case when da.morning>0 then ‘早":"‘||to_char(da.morning) end as morningNum,
case when da.noon>0 then ‘中":"‘||to_char(da.noon) end as noonNum,
case when da.night>0 then ‘晚":"‘||to_char(da.night) end as nightNum,
case when da.other>0 then ‘其":"‘||to_char(da.other) end as otherNum,
case when da.zheng1>0 then ‘正1":"‘||to_char(da.zheng1) end as zheng1Num,
case when da.zheng2>0 then ‘正2":"‘||to_char(da.zheng2) end as zheng2Num
from (
select du.duty_user,
sum(case when du.duty_schedule like ‘早%‘ then 1 end) as morning,
sum(case when du.duty_schedule like ‘晚%‘ then 1 end) as night,
sum(case when du.duty_schedule like ‘中%‘ then 1 end) as noon,
sum(case when du.duty_schedule=‘其他‘ then 1 end) as other,
sum(case when du.duty_schedule=‘正1‘ then 1 end) as zheng1,
sum(case when du.duty_schedule=‘正2‘ then 1 end) as zheng2
from crm_duty du where du.is_mark=‘1‘ and du.duty_user like ‘A%‘
and du.duty_time>=to_date(‘2017-06-01 00:00:00‘,‘yyyy-mm-dd hh24":"mi":"ss‘)
and du.duty_time<=to_date(‘2017-06-20 23:59:59‘,‘yyyy-mm-dd hh24":"mi":"ss‘)
group by du.duty_user

)da,crm_user u
where da.duty_user=u.user_code
)
select t2.*, t.*
from table( pivot_shx(‘select
duty_user , to_char(duty_time,‘‘yyyy-mm-dd‘‘) as dtime ,duty_schedule from crm_duty d
where d.is_mark=‘‘1‘‘
and d.duty_time>=to_date(‘‘2017-06-01 00:00:00‘‘,‘‘yyyy-mm-dd hh24":"mi":"ss‘‘)
and d.duty_time<=to_date(‘‘2017-06-20 23:59:59‘‘,‘‘yyyy-mm-dd hh24":"mi":"ss‘‘) ‘)) t, tmp_tab t2
where t.duty_user=t2.ucode

时间: 2024-10-10 08:23:30

oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了的相关文章

Oracle行转列(使用pivot函数)

在日常使用中,经常遇到这样的情况,需要将数据库中行转化成列显示,如 转化为 这个时候,我们就需要使用pivot函数 百度后,参考网址http://www.2cto.com/database/201501/367164.html ,完成了以下操作 with temp as( select '四川省' nation ,'成都市' city,'第一' ranking from dual union all select '四川省' nation ,'绵阳市' city,'第二' ranking fro

Oracle中使用Table()函数解决For循环中不写成 in (l_idlist)形式的问题

转: Oracle中使用Table()函数解决For循环中不写成 in (l_idlist)形式的问题 在实际PL/SQL编程中,我们要对动态取出来的一组数据,进行For循环处理,其基本程序逻辑为: 1 2 3 4 5 6 7 8 9 10 11 12 create or replace procedure getidlist is   l_idlist varchar2(200); begin   l_idlist:='1,2,3,4';   for brrs in (select * fro

oracle 使用pivot 函数进行批量数据的动态行转列

公司业务要求,需要对一批数据大批量地进行动态行转列之后进行分析,而pivot 函数in中不能使用子查询,有个方法是说可以通过动态sql拼接的方法实现, 在网上找了很久之后没找到具体的方法,就自己去摸索实现了.具体方法如下. 因为需要实现的是动态行转列,所以需要先用wm_concat(distinct())函数单独查出列名,再用dbms_lob.substr()将clob字段转成字符串.然后动态拼接得出的列名来进行查询.建个视图存放查询的结果就可以直接查需要的表啦.具体的代码如下: PROCEDU

oracle中WMSYS.WM_CONCAT函数的版本差异

昨天在测试的时候发现,开发人员写的一段程序放在开发库中是好的,但是放在测试库中就会有问题.开发人员一直找不到问题的原因在哪里.于是就花了点时间协助开发人员来找问题的根本原因. 通过一些技术手段,定位到问题是由一个SQL语句引起的. SQL语句如下: WITH PXQ_TASK_TEMP AS (SELECT A.DISTRIBUTE_DATE,          A.APP_NO,          A.TASK_ID,          A.TASK_NO,          A.ACTUAL

[转帖]达梦数据库(DM6)和ORACLE 10g的异同点

达梦数据库(DM6)和ORACLE 10g的异同点    https://bbs.aliyun.com/detail/351337.html 花花浪子 级别: 小白 发帖 0 云币 -41 加关注 写私信 只看楼主 更多操作楼主  发表于: 2017-03-24 比较惭愧,在当上本版版主后一直没有贡献一篇有营养的帖子,由于手上正好有达梦数据 DM6的版本,加上对ORACLE 10G比较熟悉,所以就这2种数据库的异同点做一个对比,也请大家不吝赐教. 对于达梦数据库,因为目前的工作是DBA,主要是对

Oracle 10g通过创建物化视图实现不同数据库间表级别的数据同步

摘自:http://blog.csdn.net/javaee_sunny/article/details/53439980 目录(?)[-] Oracle 10g 物化视图语法如下 实例演示 主要步骤 在A节点创建原表和物化视图日志 在B节点创建连接A节点的远程链接 在B节点处创建目标表和与目标表名称相同的物化视图 在B节点处刷新物化视图 升级采用存储过程定时任务JOB方式定时刷新物化视图 进一步优化 文章更新记录 参考文章 Oracle 10g 物化视图语法如下: create materia

[转]Oracle 10g及pro*c相关问题及解决方法(转)

Oracle 10g及pro*c相关问题及解决方法 2008年08月21日 星期四 上午 11:21 最近一直在进行ORACLE 10g和PRO*C的学习. 其中遇到了不少的问题: 现列于此,已备他用. [注:我的linux版本是RHEL 5,Oracle版本是10g] 1.在ORACLE 10g 安装准备的过程中:缺少libXp.so.6依赖 上网搜过不少文章,但是都不是很好的解决 我自己摸索出一个解决方法: 在RHEL5的安装盘中找到libXp-1.0.0-8.i386.rpm,进行安装后,

Oracle 10g安装64位图解流程

1. 安装准备阶段 1.1 安装Oracle环境 本例使用X-Manager来实现与Linux系统的连接,本例使用的所有命令和操作都是在X-Manager下进行.X-Manager安装完成后的配置方法如下: 1. 打开X-Manager的X-Shell 2. 点击New,新建一个连接地址,设置完成后,点击OK 3. 使用用户名,密码进行登录,登录完成后,进入如下图所示画面即成功连接到Linux系统 4. 4 4. 将下载的Oracle数据库的安装包10201_database_linux_x86

Redhat 5.4 + ASM + RAW+ Oracle 10g RAC 安装文档

基于Redhat 5 和redhat 4 的RAC 区别在对raw的处理上. 之前做的是redhat 4.7 的 RAC 实验. 准备做一个Redhat 5.4 的环境. RAC 的一些理论东西: http://blog.csdn.net/tianlesoftware/archive/2010/02/27/5331067.aspx RAW+ASM+ REDHAT 4.7 RAC 文档 http://blog.csdn.net/tianlesoftware/archive/2010/02/27/5