DB2数据多行数据转换成一行数据

WITH rs as
 (select name, rownumber() over() RN from T_test),
RPL(RN,
name) AS
 (select ROOT.RN, CAST(ROOT.name AS VARCHAR(2000))
    from rs ROOT
  UNION ALL
  SELECT CHILD.RN, CHILD.name || ',' || PARENT.name
    FROM RPL PARENT, rs CHILD
   WHERE PARENT.RN + 1 = CHILD.RN)
select MAX(name) name
  from RPL
 GROUP BY RN
 ORDER BY RN DESC fetch first 1 rows only

你可能感兴趣的:(db2)