有下表my_tabe
:
M01 | 1
M01 | 2
M02 | 1
我想查询它以获得:
M01 | 1,2
M02 | 1
我设法使用以下查询接近:
with my_tabe as
(
select 'M01' as scycle, '1' as sdate from dual union
select 'M01' as scycle, '2' as sdate from dual union
select 'M02' as scycle, '1' as sdate from dual
)
SELECT scycle, ltrim(sys_connect_by_path(sdate, ','), ',')
FROM
(
select scycle, sdate, rownum rn
from my_tabe
order by 1 desc
)
START WITH rn = 1
CONNECT BY PRIOR rn = rn - 1
产量:
SCYCLE | RES
M02 | 1,2,1
M01 | 1,2
这是错误的。看来我已经很接近了,但恐怕我不知道下一步是什么......
有小费吗?