Oracle combines multiple rows into one with a separate header

I have this statement that concatenates several lines into one, and then outputs it the way I need the data:

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; 

Result:

 ╔═══════════╦════════════╦═════════════════════════════════════╦═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ ║ ORDER_NUM ║ COURSES_ID ║ REQUISITE_TYPE_TITLE ║ REQ_TEXT ║ ╠═══════════╬════════════╬═════════════════════════════════════╬═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣ ║ 11175 ║ Limitation on Enrollment ║ With minimum grade of "C" ║ ║ 21175 ║ 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 ║ ╚═══════════╩════════════╩═════════════════════════════════════╩═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ 

Composition:

 ╔═══════════╦════════════╦═══════════╦═════════════════════════════════════╦══════════════════════════════════════════════════════════╗ ║ 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 ║ ║ 11651175 ║ ║ Advisory on Recommended Preparation ║ or equivalent college course with "C" or better ║ ║ 11661175or ║ Advisory on Recommended Preparation ║ MATH 205 or equivalent college course with "C" or better ║ ║ 11671175or ║ Advisory on Recommended Preparation ║ or equivalent college course with "C" or better ║ ║ 11681175 ║ ║ Advisory on Recommended Preparation ║ or equivalent college course with "C" or better ║ ║ 11691175 ║ ║ Limitation on Enrollment ║ With minimum grade of "C" ║ ╚═══════════╩════════════╩═══════════╩═════════════════════════════════════╩══════════════════════════════════════════════════════════╝ 

Required Result:

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

I try not to put this in a temporary table or multiple views that display it in one section.

I know that he has bad data, but they told me that Garbage In, Garbage Out. Any help would be greatly appreciated.

Even with LISTAGG, which does not allow to distinguish, as far as I can tell:

SELECT COURSES_ID, LISTAGG (REQUISITE_TYPE_TITLE || '' || CONDITION_TITLE || '' || REQ_TEXT, '') IN GROUP (ORDER ORDER_NUM) AS Title FROM COS_REQUISITES WHERE COURSES_ID = '1175';

Result:

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

Pay attention to the REQUISITE_TYPE_TITLE repeat, I want it to be displayed once as the desired result. I cannot modify the table to have their null value due to the fact that it is a table loaded dynamically and updated by someone else daily.

+6
source share
2 answers

a little dirty but should give what you asked

 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; 
+1
source

Try using the LISTAGG () function

For example: http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php

Hth

+1
source

Source: https://habr.com/ru/post/897427/


All Articles