ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【笔记】oracle中多行合并

【笔记】oracle中多行合并

原创 Linux操作系统 作者:klamm0 时间:2009-11-23 15:39:53 0 删除 编辑

自定义一个聚合函数

1.创建类型

create or replace type strcat_type as object (
cat_string varchar2(4000),
static function ODCIAggregateInitialize(cs_ctx In Out strcat_type) return number,
member function ODCIAggregateIterate(self In Out strcat_type,value in varchar2) return
number,
member function ODCIAggregateMerge(self In Out strcat_type,ctx2 In Out strcat_type)
return number,
member function ODCIAggregateTerminate(self In Out strcat_type,returnValue Out
varchar2,flags in number) return number
);

2. 创建类型体

create or replace type body strcat_type is
static function ODCIAggregateInitialize(cs_ctx IN OUT strcat_type) return number
is
begin
      cs_ctx := strcat_type( null );
      return ODCIConst.Success;
end;

member function ODCIAggregateIterate(self IN OUT strcat_type,
                                       value IN varchar2 )
return number
is
begin
      self.cat_string := self.cat_string || ';'|| value;
      return ODCIConst.Success;
end;

member function ODCIAggregateTerminate(self IN Out strcat_type,
                                         returnValue OUT varchar2,
                                         flags IN number)
return number
is
begin
      returnValue := ltrim(rtrim(self.cat_string,';'),';');
      return ODCIConst.Success;
end;

member function ODCIAggregateMerge(self IN OUT strcat_type,
                                     ctx2 IN Out strcat_type)
return number
is
begin
      self.cat_string := self.cat_string || ';' || ctx2.cat_string;
      return ODCIConst.Success;
end;

end;

3.创建函数
CREATE OR REPLACE FUNCTION strcat(input varchar2 )
RETURN varchar2
PARALLEL_ENABLE AGGREGATE USING strcat_type;

4、select id, strcat(name) from users group by id;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22786751/viewspace-620325/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-10-30

  • 博文量
    58
  • 访问量
    58108