1.wm_concat()函数是oracle内部用户下的函数,通常不建议使用,而listagg()函数只在oracle11g下才有,这时候就需要自定义合并列的聚集函数,具体sql如下:
CREATE OR REPLACE TYPE T_LINK AS OBJECT
(
STR VARCHAR2(32767),STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER,MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK,VALUE IN VARCHAR2) RETURN NUMBER,MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK,RETURNVALUE OUT CLOB,FLAGS IN NUMBER)
RETURN NUMBER,MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK,CTX2 IN T_LINK) RETURN NUMBER
)
/
CREATE OR REPLACE TYPE BODY T_LINK IS
STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER IS
BEGIN
SCTX := T_LINK(NULL);
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK,VALUE IN VARCHAR2) RETURN NUMBER IS
BEGIN
SELF.STR := SELF.STR || VALUE || ',';
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK,FLAGS IN NUMBER) RETURN NUMBER IS
BEGIN
RETURNVALUE := SUBSTR(SELF.STR,1,LENGTH(SELF.STR) - 1);
RETURN ODCICONST.SUCCESS;
END;
MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK,CTX2 IN T_LINK) RETURN NUMBER IS
BEGIN
NULL;
RETURN ODCICONST.SUCCESS;
END;
END;
/
CREATE OR REPLACE FUNCTION F_LINKS(P_STR VARCHAR2) RETURN CLOB
AGGREGATE USING T_LINK;
/
把这段sql在oracle中运行一下,就可以使用了,具体使用示例如下:
select F_LINKS(t.name)
from PHY_DEVICE_TBL t
where instr(c.deviceid,to_char(t.id)) > 0
合并的结果列之间是用逗号分隔的!