【发布时间】:2019-02-13 21:20:18
【问题描述】:
我正在尝试使用 Oracle LISTAGG 创建基于行的串联值字符串。这是仍然显示我正在尝试解决的问题的简化代码。
SELECT S_SUBJECT.UNIQRECNUM,
(SELECT LISTAGG(S_STUDY.U_PRIMARYRESEARCHCOHORT,',') WITHIN GROUP (
ORDER BY U_PRIMARYRESEARCHCOHORT)
FROM S_STUDY
WHERE S_STUDY.S_STUDYID = S_PARTICIPANT.SSTUDYID) COHORTS
FROM S_SUBJECT
JOIN S_PARTICIPANT ON S_PARTICIPANT.SUBJECTID = S_SUBJECT.S_SUBJECTID
WHERE S_SUBJECT.UNIQRECNUM IN ('9','8','7','6','5','2');
These are the results of this query;
UNIQRECNUM | COHORTS
---------------------
2 | Gastro
5 | Metabolic
6 | Cardio
7 | Cardio
8 | Gastro
8 | Cardio
9 | Gastro
9 | Gastro
9 | Gastro
What I am expecting to see from LISTAGG would be
UNIQRECNUM | COHORTS
--------------------
2 | Gastro
5 | Metabolic
6 | Cardio
7 | Cardio
8 | Cardio,Gastro
8 | Cardio,Gastro
9 | Gastro,Gastro,Gastro
9 | Gastro,Gastro,Gastro
9 | Gastro,Gastro,Gastro
【问题讨论】: