If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Combining multiple values into one - Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-11, 15:20
dyer dyer is offline
Registered User
 
Join Date: Jun 2011
Posts: 6
Combining multiple values into one - Help

Hey Guys, Junior DB2 DBA here

An interesting issue im having where i have been asked to return a single string containing all of the codes associated with a specific ID.

So Data could be (Single table)

ID Code
--- -------
1 ABC
2 ABF
3 CDE
1 DLE
1 PFS

And say the request was for ID = 1 the desired result is to have the data look like.

ID Code
-- -------
1 ABC, DLE, PFS

I have Googled my heart out and all i can find is "Recursive should work" but have yet been able to find an example.

I have seen a few on these forums specifically this one

Combine Multiple Columns into single Coma seperated string result.

But am having troubles converting this into what im after as i dont quite understand the logic.

IF anyone could help me out even with a starting point i would be greatful.

Cheers
Reply With Quote
  #2 (permalink)  
Old 11-09-11, 18:30
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
This is a FAQ and is asked here more or less once per week.

One example for a solution is here: Script Library

You can also search for XMLAGG and "string concatenation" to find another solution.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 11-09-11, 18:50
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
You can use LISTAGG on DB2 9.7 fixpack 4 for LUW.

http://publib.boulder.ibm.com/infoce.../r0058709.html

Last edited by tonkuma; 11-09-11 at 18:51. Reason: Add reference.
Reply With Quote
  #4 (permalink)  
Old 11-09-11, 19:20
dyer dyer is offline
Registered User
 
Join Date: Jun 2011
Posts: 6
9.7.2

Sadly im not on 9.7.4 and only on FP2.

I have come up with this below, but need to somehow turn the decleration of "text" into a larger field as it is picking up the limits of the "code" column

Run SQL

WITH result(File_id, text) AS (
SELECT File_id, FL_CODE
FROM schema.file
WHERE FILE_ID = '7000'
UNION ALL
SELECT c.File_id, r.text ||', '|| FL_CODE
FROM schema.file c, result r
)
SELECT File_id, text from result
FETCH FIRST 1 ROW ONLY

Value "XM01, OR05" is too long.. SQLCODE=-433, SQLSTATE=22001, DRIVER=4.11.69

Failed queries => 1

Total execution time => 0 ms
Reply With Quote
  #5 (permalink)  
Old 11-09-11, 20:36
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Cast it to a CLOB, may be?
Reply With Quote
  #6 (permalink)  
Old 11-09-11, 20:46
dyer dyer is offline
Registered User
 
Join Date: Jun 2011
Posts: 6
Solved!

Have found a solution here!

works well

DB2 Diary from Radhesh: SQL Tips & Techniques- String Aggregation

Thanks guys
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On