Oracle将多行合并为一个具有不同标题的行

Ahs*_*enB 6 sql database oracle

我有这个语句将多行合并为一个,然后以我需要的方式输出它:

 SELECT COURSES_ID, REQUISITE_TYPE_TITLE
            , RTRIM
           ( xmlagg (xmlelement (c, CONDITION_TITLE || '' || REQ_TEXT || ''  ) order by   ORDER_NUM).extract ('//text()')
           , ',' ) AS REQTexts
    FROM   COS_REQUISITES
    WHERE COURSES_ID = '1175'
    AND REQUISITE_TYPE_TITLE !=  'Corequisite'
    GROUP BY COURSES_ID, REQUISITE_TYPE_TITLE;
Run Code Online (Sandbox Code Playgroud)

结果:

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? ORDER_NUM ? COURSES_ID ?        REQUISITE_TYPE_TITLE         ?                                                                                                                                                           REQ_TEXT                                                                                                                                                            ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?         1 ?       1175 ? Limitation on Enrollment            ?   With minimum grade of "C"                                                                                                                                                                                                                                                                                         ?
?         2 ?       1175 ? Advisory on Recommended Preparation ? MATH 200 or equivalent college course with "C" or better  or equivalent college course with "C" or better or MATH 205 or equivalent college course with "C" or better or   or equivalent college course with "C" or better  or equivalent college course with "C" or better ?
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

结构体:

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? ORDER_NUM ? COURSES_ID ? CONDITION ?        REQUISITE_TYPE_TITLE         ?                         REQ_TEXT                         ?
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?      1164 ?       1175 ?           ? Advisory on Recommended Preparation ? MATH 200 or equivalent college course with "C" or better ?
?      1165 ?       1175 ?           ? Advisory on Recommended Preparation ?   or equivalent college course with "C" or better        ?
?      1166 ?       1175 ?  or       ? Advisory on Recommended Preparation ? MATH 205 or equivalent college course with "C" or better ?
?      1167 ?       1175 ?  or       ? Advisory on Recommended Preparation ?   or equivalent college course with "C" or better        ?
?      1168 ?       1175 ?           ? Advisory on Recommended Preparation ?   or equivalent college course with "C" or better        ?
?      1169 ?       1175 ?           ? Limitation on Enrollment            ?   With minimum grade of "C"                              ?
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

通缉结果:

?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Advisory on Recommended Preparation MATH 200 or equivalent college course with "C" or better  or equivalent college course with "C" or better or MATH 205 or equivalent college course with "C" or better or equivalent college course with "C" or better  or equivalent college course with "C" or better Limitation on Enrollment With minimum grade of "C" ?
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

我试图避免将其放在临时表或多个视图中,将其输出到一个部分.

我知道它有糟糕的数据,但我被告知Garbage In,Garbage Out.任何帮助将不胜感激.

即使是LISTAGG,据我所知,它不允许区分:

SELECT COURSES_ID,LISTAGG(REQUISITE_TYPE_TITLE ||''|| CONDITION_TITLE ||''|| REQ_TEXT,'')WITHIN GROUP(ORDER BY ORDER_NUM)AS标题来自COS_REQUISITES,其中COURSES_ID ='1175'GROUP BY COURSES_ID;

结果:

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?                                                                                                                                                                                                                                                             TITLE                                                                                                                                                                                                                                                              ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
? Advisory on Recommended Preparation  MATH 200 or equivalent college course with "C" or better Advisory on Recommended Preparation    or equivalent college course with "C" or better Advisory on Recommended Preparation  or  MATH 205 or equivalent college course with "C" or better Advisory on Recommended Preparation  or    or equivalent college course with "C" or better Advisory on Recommended Preparation    or equivalent college course with "C" or better Limitation on Enrollment    With minimum grade of "C" ?
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

注意REQUISITE_TYPE_TITLE重复,我只希望它像想要的结果一样出现.我无法修改表以使它们为null,因为这是一个动态加载并由其他人每天更新的表.

Yah*_*hia 1

有点乱,但应该能满足你的要求

SELECT 
COURSES_ID, 
RTRIM ( xmlagg (xmlelement (c, REQUISITE_TYPE_TITLE || '' || REQTexts || ''  ) order by   mino).extract ('//text()'), ',' ) AS REQTexts
FROM
(
SELECT 
COURSES_ID, REQUISITE_TYPE_TITLE, MIN (ORDER_NUM) mino, 
RTRIM ( xmlagg (xmlelement (c, CONDITION_TITLE || '' || REQ_TEXT || ''  ) order by   ORDER_NUM).extract ('//text()') , ',' ) AS REQTexts
FROM COS_REQUISITES
WHERE COURSES_ID = '1175'
AND REQUISITE_TYPE_TITLE !=  'Corequisite'
GROUP BY COURSES_ID, REQUISITE_TYPE_TITLE
) 
GROUP BY COURSES_ID;
Run Code Online (Sandbox Code Playgroud)