2013-12-17 36 views
2

我有这个clobagg功能:如何使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

我尝试按照正确的顺序将活动列添加到表中。为了这一刻,我可以添加到表中,但随机顺序。当我尝试通过aktiv.AKTIVITÄT_NR尝试订单数据时,我还必须将此字段添加到我的组中,这会破坏我的分组。对不起,这很多部分。如果您愿意,我可以通过电子邮件向您发送所有查询 – Greg

+0

您在查询中使用了'clobagg()'两次;都是按照你不喜欢的顺序产生输出,或者只是外部输出? –

回答

3

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

+1

你能解释一下我应该怎么做吗? – Greg

相关问题