Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subselect - Resultset in a comma separated row

Status
Not open for further replies.

galaxy0815

Technical User
Dec 27, 2006
39
US
Hello,

Please be advised that I have a complex SQL Statement that joins various tables. Within this statement I want to do the following (not sure if this is possible):

As 3rd field in the select part of that complex SQL statement, I want to enter a subselect that runs over a table called "car_availablility" which has the following layout:

Table car_availablility layout
Car -------- Owner -------------Currently_available
BMW -------- Dirk -------------- YES
VW --------- Dirk -------------- YES
Skoda ------ Dirk -------------- YES
Ford ------- Dirk -------------- NO
Seat ------- Marco ------------- YES
VW --------- Marco ------------- NO
Opel ------- Marco ------------- YES

the subselect looks as follows (with a link to the a.Owner field from the main complex select statement)
(select Car from car_availablility where a.Owner = car_availablility.Owner and Currently_available = 'YES') as Car_Overview


The issue is now the following:
Instead of having the result from the subselect shown on multiple lines, I want to list the values behind each other comma separated within the same field.

Example:
Instead of having the cars for Dirk listed as this:
Car_Overview
BMW
VW
Skoda

I want to have them listed as
Car_Overview
BMW, VW, Skoda


Is this possible just within a select statement (I cannot use stored procedures etc as my user just has select privileges)

Systeminfo:
Database server = DB2/NT64 9.5.8


Thanks a lot in advance!
Regards,
 
LOOKS LIKE NOT POSSIBLE BY USING SQL; PLEASE LET ME KNOW, IF YOU HAVE SOLUTION. THANKS.
 
Hi,

Still trying on my side, but maybe try with one of the below:

Code:
SELECT replace(replace(XMLSERIALIZE(CONTENT xmlagg(xmlelement(Name a, CAR)) AS VARCHAR(32599)),'<A>',''),'</A>','; ')
FROM car_availablility

or

Code:
SELECT replace(replace(xml2clob(xmlagg(xmlelement(NAME a, car))),'<A>',''),'</A>',' ')
FROM car_availablility

They should work, but not sure if this is the best way.


Regards,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top