2011-09-15 36 views
6

tengo esta declaración que combina múltiples filas en una y luego la emite en la forma en que necesito los datos:Oracle combinar varias filas en una con título distinto

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; 

Resultado:

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

estructura:

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

resultado deseado:

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

Estoy tratando de evitar tener que poner esto en una tabla temporal o varias vistas que mostrarían esto en una sección.

Sé que tiene datos erróneos pero me dijeron que Garbage In, Garbage Out. Cualquier ayuda sería muy apreciada.

Incluso con LISTAGG que no permite de distinta por lo que yo puedo decir:

SELECT COURSES_ID, LISTAGG (REQUISITE_TYPE_TITLE || '' || CONDITION_TITLE || '' || REQ_TEXT, ' ') DENTRO DEL GRUPO (ORDEN POR ORDER_NUM) AS Título DESDE COS_REQUISITES DONDE COURSES_ID =' 1175 ' GROUP BY COURSES_ID;

Resultado:

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

Aviso REQUISITE_TYPE_TITLE las repeticiones, sólo quieren que aparece una vez como el resultado deseado. No puedo modificar la tabla para que sean nulas debido a que esta es una tabla que se carga de forma dinámica y que otra persona actualiza diariamente.

+3

Wow, pseudográfica! ¡Mucho tiempo sin verte! (Fuera de mi instalación de Far Manager, es decir). Fingiré que estoy haciendo una pregunta útil, bien escrita e ilustrada, y no solo estas hermosas tablas. :) –

+1

gracias ... Encontré este enlace útil para hacer esto: http://www.sensefulsolutions.com/2010/10/format-text-as-table.html Copie los datos a excel en la tabla encabezados, etc. Seleccione Arte Unicode y listo. También como se muestra: http://webapps.stackexchange.com/questions/6700/is-there-a-webapp-to-create-ascii-art-tables y http://meta.stackexchange.com/questions/ 83009/hacer-usted-sabe-cualquier-herramienta-en-línea-que-crea-ascii-tablas-para-uso-apropiado-en-pila-exc – AhsenB

+0

¡Qué gran herramienta, gracias! –

Respuesta

1

un poco desordenado, pero debe ceder lo que pidió

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; 
Cuestiones relacionadas