如何使CLOBAGG函数排序结果?

时间:2013-12-17 09:52:39

标签: oracle11g aggregate-functions

我有这个clobagg功能:

create or replace type clobagg_type as object(
  text clob,
  static function ODCIAggregateInitialize(sctx in out clobagg_type
                                         ) return number,
  member function ODCIAggregateIterate(self in out clobagg_type,
                                       value in clob
                                      ) return number,
  member function ODCIAggregateTerminate(self in clobagg_type,
                                         returnvalue out clob,
                                         flags in number
                                        ) return number,
  member function ODCIAggregateMerge(self in out clobagg_type,
                                     ctx2 in clobagg_type
                                    ) return number
);
/
create or replace type body clobagg_type is
  static function ODCIAggregateInitialize(sctx in out clobagg_type
                                         ) return number is
  begin
    sctx := clobagg_type(null);
    return ODCIConst.Success;
  end;
  member function ODCIAggregateIterate(self  in out clobagg_type,
                                       value in     clob
                                      ) return number is
  begin
    self.text := self.text || value;
    return ODCIConst.Success;
  end;
  member function ODCIAggregateTerminate(self in clobagg_type,
                                         returnvalue out clob,
                                         flags in number
                                        ) return number is
  begin
    returnValue := self.text;
    return ODCIConst.Success;
  end;
  member function ODCIAggregateMerge(self in out clobagg_type,
                                     ctx2 in clobagg_type
                                    )return number is
  begin
    self.text := self.text || ctx2.text;
    return ODCIConst.Success;
  end;
end;
/
create or replace function clobagg(input clob) return clob
  deterministic
  parallel_enable
  aggregate using clobagg_type;
/

但问题是我的数据输入顺序不正确。你能帮助我并告诉我如何实现正确的秩序吗?我需要clobagg函数,因为listagg和其他可以返回4000个字节,在我的情况下它是不够的。


以下是查询:

CREATE TABLE GO_PRJ_SACHV7.TEST_STEPS1 (
    test_case_id NUMBER(9,0),
    activity CLOB
);

INSERT INTO GO_PRJ_SACHV7.TEST_STEPS(test_case_id, activity)
  select test_case_id, clobagg(activity1)
  from (
    select 
    testschrit.testfall_id as test_case_id,
    TESTSCHRITT_NR,
    CHR(10) || 'h2.' || TESTSCHRITT_NR || ' ' ||
      CAST(TESTSCHRITT_BEZEICHNUNG AS varchar(800)) || CHR(10) ||
      CAST(TESTSCHRITT_BESCHREIBUNG AS varchar(800)) || CHR(10) ||
      CAST(testschrit.TESTSCHRITT_BESCHREIBUNG AS varchar(800)) ||
      '||AKTIVITÄT_NR' || '||AKTIVITÄT_KÜRZEL' || '||AKTIVITÄT_BESCHREIBUNG' ||
      '||AKTIVITÄT_ERWARTETES_ERGEBNIS||' ||  CHR(10)  ||
      clobagg(
        ' |' || aktiv.AKTIVITÄT_NR || ' |' || aktiv.AKTIVITÄT_KÜRZEL || ' |' ||
        aktiv.AKTIVITÄT_BESCHREIBUNG || ' |' ||
        aktiv.AKTIVITÄT_ERWARTETES_ERGEBNIS || ' |' ||  CHR(10)
      ) as activity1
    FROM
      GO_PRJ_SACHV7.TESTFALLBESCHREIBUNG tfb,
      GO_PRJ_SACHV7.TESTSCHRITTE testschrit,
      GO_PRJ_SACHV7.AKTIVITÄTEN aktiv
    WHERE testschrit.testfall_id = tfb.testfall_id(+)
    AND  testschrit.TESTSCHRITT_ID=aktiv.TESTSCHRITT_ID (+)
    Group by 
    testschrit.testfall_id,
    testschrit.testschritt_id,
    testschrit.TESTSCHRITT_NR,
    CAST(TESTSCHRITT_BEZEICHNUNG AS varchar(600)),
    CAST(TESTSCHRITT_BESCHREIBUNG AS varchar(600))
    order by testschrit.testfall_id, TESTSCHRITT_NR
  )
group by test_case_id;  

我尝试按正确的顺序将“活动”列添加到表中。这一刻我可以将它添加到表中,但是随机顺序。当我通过aktiv.AKTIVITÄT_NR尝试订单数据时,我还必须将此字段添加到我的组中,这会破坏我的分组。

1 个答案:

答案 0 :(得分:3)

你错过了内部聚合的订单。您需要在汇总之前订购。你在外部聚合之前做到这一点,而不是在内部聚合之前。