Oracle中合并数据集(多行变一行)
Oracle中合并数据集(多行变一行),表A和表B之间为一对多的关系。
需求:
查询表A的全部数据,并将表A对应的表B数据的某个项目合并为一行,以[,]分隔。
ORACLE FUNCTION
CREATE OR REPLACE FUNCTION FUN_GETNAME (
ID IN NUMBER
) RETURN VARCHAR2
IS
RESULT_OUT VARCHAR2(20000);
BEGIN
DECLARE CURSOR C_CUR IS SELECT CTASKID FROM TABLE_B WHERE CID = ID ORDER BY CTASKID;
BEGIN
FOR ROWSTR IN C_CUR LOOP
RESULT_OUT := RESULT_OUT || ',' || ROWSTR.CTASKID;
END LOOP;
END;
RESULT_OUT := SUBSTR(RESULT_OUT, 2);
RETURN RESULT_OUT;
END;
/
查询数据的SQL:
SELECT A.ID, A.NAME, A.FLAG, A.DELETE_FLAG, FUN_GETNAME(A.CODE) AS SCH FROM TABLE_A A ORDER BY A.ID, SCH
相关推荐
lklong 2020-11-22
oraclemch 2020-11-06
shilukun 2020-10-10
周嘉笙 2020-11-09
iilegend 2020-10-19
EricRay 2020-10-16
zhuzhufxz 2020-09-16
dataminer 2020-08-17
bfcady 2020-08-16
Hody 2020-08-16
FightFourEggs 2020-08-16
数据库设计 2020-08-16
Seandba 2020-08-16
Omega 2020-08-16
zjyzz 2020-08-16
yanghuatong 2020-08-16
ktyl000 2020-08-16
dbasunny 2020-08-16