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 > How to aggregate stings in UDB DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-09-07, 15:31
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
How to aggregate stings in UDB DB2

I have following sample data
COL1 COL2
----------- ----------
1 Welcome
1 udb
1 database
2 Toronto
2 canada

I want to run a aggreagte function on col2 in such a manner , my select output should be

1 welcome udb database
2 toronto canda

Thx For help
Reply With Quote
  #2 (permalink)  
Old 08-09-07, 19:28
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
I discussed with my co-work before for similar question, and the conclusion is that there is no way to do this in a single standard sql query(no external UDF or stored proc). And should be fair easy and efficient by using an application. (for example StringBuffer class in java).

Anyway, with the latest XML functions, it is doable with XMLAGG and XMLQUERY functions, such as(run on db2/zos):


CREATE TABLE TT
(COL1 VARCHAR(10));

INSERT INTO TT VALUES('welcome');
INSERT INTO TT VALUES('udb');
INSERT INTO TT VALUES('db2base');

SELECT
XMLSERIALIZE(
XMLQUERY('/fn:data(COL1)' PASSING
XMLDOCUMENT(
XMLAGG(XMLELEMENT(NAME "COL1", COL1))) )
AS CLOB(50) EXCLUDING XMLDECLARATION)
FROM TT;


+----------------------------------------------------+
| |
+----------------------------------------------------+
1_| welcome udb db2base |
+----------------------------------------------------+


DB2/LUW should support similar functions, but you need V9.

Anyway, I won't suggest you use above method(which suffer too much overhead when deal with xml).
Hope someone else has smarter ideas.

Last edited by nidm; 08-09-07 at 20:05.
Reply With Quote
  #3 (permalink)  
Old 08-10-07, 01:58
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
This can be achieved with recursive SQL using CTEs; see e.g. the threads http://www.dbforums.com/ansi-sql/1612892-query-special-transpose-merge-data.html#post6248263, Concatenate column values from multiple rows and How to concat several lines into one
Note that you need to specify the concatenation order in some way or the other, e.g. through a third column. (Aggregate SUM, COUNT etc. are order-indifferent, CONCAT isn't.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 08-10-07 at 02:03.
Reply With Quote
  #4 (permalink)  
Old 08-10-07, 02:52
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by nidm
I discussed with my co-work before for similar question, and the conclusion is that there is no way to do this in a single standard sql query(no external UDF or stored proc).
Wrong conclusion.

Besides the recursive queries that Peter referred to, you can use DB2's XML capabilities and the XMLAGG function. Here is an example:
http://www.mydatabasesupport.com/for...cte-udf.html#6
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 08-10-07, 11:39
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
Quote:
Originally Posted by stolze
Wrong conclusion.

Besides the recursive queries that Peter referred to, you can use DB2's XML capabilities and the XMLAGG function. Here is an example:
http://www.mydatabasesupport.com/for...cte-udf.html#6
I got confused from your comments. Didn't I provide an xmlagg example in the original comments?

I don't think standard sql including xml tech. XML is more like another tech(other than sql), but happened to be implemented by IBM inside db2.
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