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

时间:2011-09-15 22:11:40

标签: 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;

结果:

╔═══════════╦════════════╦═════════════════════════════════════╦═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ 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 ║
╚═══════════╩════════════╩═════════════════════════════════════╩═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

结构:

╔═══════════╦════════════╦═══════════╦═════════════════════════════════════╦══════════════════════════════════════════════════════════╗
║ 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"                              ║
╚═══════════╩════════════╩═══════════╩═════════════════════════════════════╩══════════════════════════════════════════════════════════╝

通缉结果:

╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ 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" ║
╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

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

我知道它有糟糕的数据,但我被告知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" ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

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

2 个答案:

答案 0 :(得分:1)

尝试使用LISTAGG()函数

例如:http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php

H个

答案 1 :(得分: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;
相关问题