WITH
t1 (id, DEPTNO, SEX, ENAME) AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY DEPTNO, SEX ORDER BY ENAME) AS ID,
DEPTNO,
SEX,
ENAME
FROM
temp
),
t2 (id, DEPTNO, SEX, ENAME) AS
(
SELECT
t1.id,
t1.DEPTNO,
t1.SEX,
CAST(t1.ENAME AS varchar(100)) AS ENAME
FROM
t1
WHERE
t1.id = 1
UNION ALL
SELECT
t1.id,
t1.DEPTNO,
t1.SEX,
CAST( t2.ENAME || ' ' || t1.ENAME AS varchar(100)) AS ENAME
FROM
t1, t2
WHERE
t1.DEPTNO = t2.DEPTNO
AND t1.SEX = t2.SEX
AND t1.id = (t2.id + 1)
)
SELECT
DEPTNO,
SEX,
ENAME
FROM
t2
WHERE
NOT EXISTS (
SELECT 1
FROM t2 t22
WHERE
t2.DEPTNO = t22.DEPTNO
AND t2.SEX = t22.SEX
AND t2.id < t22.id
);
DEPTNO SEX ENAME
----------- -------------------- -----------------------------------------------
-----------------------------------------------------
SQL0347W 递归公共表表达式 "TEST.T2" 可能包含无限循环。 SQLSTATE=01605
10 F aaa bbb ccc
20 M ddd eee fff
30 X ggg hhh ttt yyy
已选择 3 条记录,打印 1 条警告消息。
db2 =>