
Signature
Knut Stolze
DB2 Information Integration Development
IBM Germany
Here's an example applying a recursive CTE, using an algorithm from
Molinaro's _SQL Cookbook_ (note: best viewed with fixed font):
WITH
PARTICIPANT_NAME
(
PRTCP_NM_ID,
PRTCP_ID,
FIRST_NM
)
AS
(VALUES (1,1,'JEFF'),
(2,1,'JEFFREY'),
(3,2,'EDDIE'),
(4,2,'EDWARD'),
(5,2,'TED'),
(6,3,'ELIZABETH')
),
PRTCP_NM
(
PRTCP_ID,
CNT,
LIST,
PRTCP_NM_ID,
LEN
)
AS
(
SELECT
PN1.PRTCP_ID,
COUNT(*) OVER (PARTITION BY PN1.PRTCP_ID),
CAST(PN1.FIRST_NM AS VARCHAR(4000)),
PN1.PRTCP_NM_ID,
1
FROM
PARTICIPANT_NAME PN1
UNION ALL
SELECT
PRTCP_NM.PRTCP_ID,
PRTCP_NM.CNT,
PRTCP_NM.LIST||', '||PN2.FIRST_NM,
PN2.PRTCP_NM_ID,
PRTCP_NM.LEN + 1
FROM
PARTICIPANT_NAME PN2,
PRTCP_NM
WHERE
PN2.PRTCP_ID = PRTCP_NM.PRTCP_ID
AND
PN2.PRTCP_NM_ID > PRTCP_NM.PRTCP_NM_ID
)
SELECT
LIST
FROM
PRTCP_NM
WHERE
LEN = CNT
and here's a really cool example--from this forum, BTW--of using the
XMLAGG function:
CREATE TABLE EMPLOYEE(NAME VARCHAR(15), DEPT VARCHAR(15));
NAME DEPT
----- ------
MISO SOLUTIONS
JOHN DEVELOPMENT
SERGE SOLUTIONS
LEE L3
MARK ID
JACK L3
LILY QUALITY
BERNI SOLUTIONS
SELECT DEPT,
SUBSTR(NAMES, 1, LENGTH(NAMES) -1)
FROM (SELECT
DEPT,
REPLACE
(REPLACE
(XMLSERIALIZE
(CONTENT XMLAGG(XMLELEMENT(NAME A, NAME)
ORDER BY NAME)
AS VARCHAR(60)), '<A>', ''), '</A>', ',') AS NAMES
FROM EMPLOYEE GROUP BY DEPT) AS X;
DEPT NAMES
----- --------------------
SOLUTIONS BERNI, MISO, SERGE
DEVELOPMENT JOHN
L3 JACK, LEE
ID MARK
QUALITY LILY
HTH,
--Jeff
> > Is there a way to bring historical data into one field? I have activities
> > A1, A2, A3 ,etc in a history table. I need to check for the presence of
[quoted text clipped - 36 lines]
> DB2 Information Integration Development
> IBM Germany
meter_man - 28 Oct 2006 06:46 GMT
Thanks you both so much. I have never even heard of recursive queries and
XMLAGGREGATE! I will be checking this out.
:)
> Here's an example applying a recursive CTE, using an algorithm from
[quoted text clipped - 138 lines]
>> DB2 Information Integration Development
>> IBM Germany