wm_concat()拼接字符串的结果只能达到4000,超过的时候需要重写一个方法,不过实际运行的时候会特别慢,这种函数只能在平时对数据进行单次查询的时候用到,在实际业务中感觉性能太差了,之前读取的数据最后分类为159组,最大组数据为3000不到个元素,总共查询时间大概有480s。
直接上sql了,事实证明是好用的,如果你的navicat不好用的话,试用下sqldeveloper试下。
create or replace TYPE zh_concat_im
AUTHID CURRENT_USER AS OBJECT
(
CURR_STR clob,
STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im) RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im,
P1 IN CLOB) RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
RETURNVALUE OUT CLOB,
FLAGS IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im,
SCTX2 IN zh_concat_im) RETURN NUMBER
);
create or replace TYPE BODY zh_concat_im
IS
STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT zh_concat_im)
RETURN NUMBER
IS
BEGIN
SCTX := zh_concat_im(NULL) ;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT zh_concat_im,
P1 IN CLOB)
RETURN NUMBER
IS
BEGIN
IF(CURR_STR IS NOT NULL) THEN
CURR_STR := CURR_STR || ',' || P1;
ELSE
CURR_STR := P1;
END IF;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN zh_concat_im,
RETURNVALUE OUT CLOB,
FLAGS IN NUMBER)
RETURN NUMBER
IS
BEGIN
RETURNVALUE := CURR_STR ;
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT zh_concat_im,
SCTX2 IN zh_concat_im)
RETURN NUMBER
IS
BEGIN
IF(SCTX2.CURR_STR IS NOT NULL) THEN
SELF.CURR_STR := SELF.CURR_STR || ',' || SCTX2.CURR_STR ;
END IF;
RETURN ODCICONST.SUCCESS;
END;
END;
create or replace FUNCTION zh_concat(P1 CLOB)
RETURN CLOB AGGREGATE USING zh_concat_im ;
附上当时执行的sql(感觉没什么用),业务上的字段我给屏蔽掉了。
SELECT A.C_ET_ID,B.C_N,COUNT(DISTINCT A.C_EN) AS NUM,zh_concat(DISTINCT A.C_EN)
FROM DU.T_S_E A
LEFT JOIN DU.T_ET B ON A.C_ET_ID = B.C_ID
GROUP BY A.C_ET_ID,B.C_N
ORDER BY NUM DESC;