How to add character to the end of the output lines by not adding to the last line?
Posting my comments as a separate answer, subquery will order the numbers and outer query will append comma based on rownum and return result in desc order
select GLOBE_MATERIAL_ID_LIST || case when rownum > 1 then ',' else '' end as GLOBE_MATERIAL_ID_LISTfrom ( SELECT DISTINCT CHR(39) || ltrim(MATNR,0) || CHR(39) AS GLOBE_MATERIAL_ID_LIST FROM PR1_SOAINFRA.WLI_NESOA2_REPORTING_ARCHIVE, XMLTABLE( '/_-GLB_-OGTX_DESADV01_CUST_ASN/IDOC/E1EDK08/E1EDP07/E1EDP09' PASSING XMLTYPE(data_value,871) COLUMNS MATNR varchar2(2000) path 'MATNR' ) WHERE ERRORCODE IS NOT NULL AND BUSINESSID = '0000000382153210-E9APRR3103' order by GLOBE_MATERIAL_ID_LIST) e order by GLOBE_MATERIAL_ID_LIST DESC
SQL Fiddle
Oracle 11g R2 Schema Setup:
create table nums( id number(10));insert into nums values ('12352682');insert into nums values ('12351436');insert into nums values ('12351434');insert into nums values ('12350683');
Query:
select num || case when rownum > 1 then ',' else '' end as numfrom (select chr(39) || id || chr(39) as numfrom nums order by num) e order by num desc
| NUM ||-------------|| '12352682', || '12351436', || '12351434', || '12350683' |
It's easy to prepend a comma to every line but the first one...
select distinct case when rownum > 1 then ',' end || globe_material_id_listfrom ( SELECT DISTINCT CHR(39) || ltrim(MATNR,0) || CHR(39) AS GLOBE_MATERIAL_ID_LIST FROM PR1_SOAINFRA.WLI_NESOA2_REPORTING_ARCHIVE, XMLTABLE( '/_-GLB_-OGTX_DESADV01_CUST_ASN/IDOC/E1EDK08/E1EDP07/E1EDP09' PASSING XMLTYPE(data_value,871) COLUMNS MATNR varchar2(2000) path 'MATNR' ) WHERE ERRORCODE IS NOT NULL AND BUSINESSID = '0000000382153210-E9APRR3103');
This has to be an inline query because of your distinct
: you need to generate the set of globe_material_id_list
before formatting it.