This largely depends on which version of DB2 you have available to you
This solution uses
LISTAGG[
^] if you have DB2 9.7 FP4 or above
If you are using a version that supports variables you could try
this[
^] (yes I know it's SQL but it should work in DB2 as well)
Or try this from
Tom Kyte[
^] - it's an Oracle solution, but again should work in DB2