zOs/SQL/EXRECURS

with a (id, nm) as
( select           1, 'apple'      from sysibm.sysDummy1
  union all select 1, 'tomato'      from sysibm.sysDummy1
  union all select 1, 'potato'      from sysibm.sysDummy1
  union all select 2, 'banana'      from sysibm.sysDummy1
  union all select 2, 'peach'       from sysibm.sysDummy1
)
, n as
(
  select a.*
       , row_number() over (partition by id order by nm) n
    from a
)
, j (m, id, nms) as
(
  select 'j', n1.id, n1.nm || value(', ' || n2.nm, '')
                           || value(', ' || n3.nm, '')
    from n n1
      left join n n2 on n1.id = n2.id and n2.n = 2
      left join n n3 on n1.id = n3.id and n3.n = 3
    where n1.n = 1
)
, r (id, nms, lv) as
(
  select id, cast(nm as varchar(50)), 1 from n where n=1
  union all select r.id, r.nms || ', ' || n.nm, lv+1
      from r join n on r.id = n.id and n.n = r.lv+1
      where lv < 999999
)
select * from j
union all select 'r', id, nms from r s
    where not exists ( select 1 from r g
                           where s.id = g.id and g.lv > s.lv)