Skip to content

Instantly share code, notes, and snippets.

@zarkosusnjar
Created January 25, 2012 11:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zarkosusnjar/1675843 to your computer and use it in GitHub Desktop.
Save zarkosusnjar/1675843 to your computer and use it in GitHub Desktop.
Aggregate concatenation - returning grouped rows as delimited list in DB2, AS400
create table t1 (num int, color varchar(10));
insert into t1 values (1,'red'), (1,'black'), (2,'red'), (2,'yellow'), (2,'green');
select num,
substr( xmlserialize( xmlagg( xmltext( concat( ', ', color ) ) ) as varchar( 1024 ) ), 3 )
from t1
group by num;
--OR without space after comma
select num,
substr( xmlserialize( xmlagg( xmltext( concat( ',', color ) ) ) as varchar( 1024 ) ), 2 )
from t1
group by num;
-- source: http://www.ibm.com/developerworks/forums/thread.jspa?threadID=143653
@zhmz1326
Copy link

zhmz1326 commented Aug 4, 2015

It was a great help! Thank you.

@ranveer-git
Copy link

It helped me.
Thanks.

@jcasaverde
Copy link

Thanks a lot, it was what i had been looking for, from many years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment