有没有一种简单的方法可以在不使用ListAgg的情况下进行字符串聚合?我是Oracle的新手,我试图使用ListAgg函数,但它无法正常工作。我想我安装的版本与ListAgg不兼容。我基本上尝试以下内容:
实际清单:
Encounter ID CPT Code
------------ ---------
123 21556
124 21554
123 92145
123 92542
输出:
Encounter ID CPT Code
------------ ---------
123 21556,92145,92542
124 21554
答案 0 :(得分:1)
这是Tom Kyte's version of Stragg,他不再使用,因为listagg效率更高。这将聚合最多4000个字符的字符串。您必须拥有在your_schema中创建类型和函数的授权。分隔符不能动态更改,但我将其留作练习。
用法:通过dept_no从emp组中选择dept_no,stragg(emp_name);
create or replace type Your_schema.stragg_type as object
(
string varchar2(4000),
static function ODCIAggregateInitialize
( sctx in out stragg_type )
return number ,
member function ODCIAggregateIterate
( self in out stragg_type ,
value in varchar2
) return number ,
member function ODCIAggregateTerminate
( self in stragg_type,
returnvalue out varchar2,
flags in number
) return number ,
member function ODCIAggregateMerge
( self in out stragg_type,
ctx2 in stragg_type
) return number
);
/
create or replace type body Your_schema.stragg_type
is
static function ODCIAggregateInitialize
( sctx in out stragg_type )
return number
is
begin
sctx := stragg_type( null ) ;
return ODCIConst.Success ;
end;
member function ODCIAggregateIterate
( self in out stragg_type ,
value in varchar2
) return number
is
begin
self.string := self.string || ',' || value ;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate
( self in stragg_type ,
returnvalue out varchar2 ,
flags in number
) return number
is
begin
returnValue := ltrim( self.string, ',' );
return ODCIConst.Success;
end;
member function ODCIAggregateMerge
( self in out stragg_type ,
ctx2 in stragg_type
) return number
is
begin
self.string := self.string || ctx2.string;
return ODCIConst.Success;
end;
end;
/
create or replace function Your_schema.stragg
( input varchar2 )
return varchar2
deterministic
parallel_enable
aggregate using stragg_type
;
/