Here is an example.
It is using XMLAGG to concatenate values of rows.
Another way is using recursive query.
You would find more examples by searching with keyword "concatenate strings" or "concatenate values" in this forum.
CREATE TABLE and populate it:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE QA
(userid INTEGER
,value VARCHAR(7)
);
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.
------------------------------ Commands Entered ------------------------------
INSERT INTO QA
VALUES
(123, 'a')
,(123, 'b')
,(123, 'c')
,(345, 'd')
,(345, 'e');
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.
CREATE FUNCTION:
Code:
------------------------------ Commands Entered ------------------------------
CREATE FUNCTION GetComments(p_userid INTEGER )
RETURNS varchar( 1024 )
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
SELECT SUBSTR(
XMLSERIALIZE(
XMLAGG(
XMLTEXT(',' || value)
ORDER BY value
)
AS VARCHAR(1024)
)
, 2)
FROM qa
WHERE userid = p_userid
;
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully.
Use of the function:
Code:
------------------------------ Commands Entered ------------------------------
SELECT userid
, VARCHAR( getcomments( userid ), 50) AS comments
FROM (VALUES 123, 345 ) q(userid)
;
------------------------------------------------------------------------------
USERID COMMENTS
----------- --------------------------------------------------
123 a,b,c
345 d,e
2 record(s) selected.