這是爲mssql編寫的,並改爲我希望的是oracle語法。請讓我知道它是否有效。
declare @t table(code int, value varchar(20))
insert @t values(1,'A0001')
insert @t values(1,'A0002')
insert @t values(2,'B0001')
insert @t values(2,'B0002')
insert @t values(2,'B0003')
insert @t values(3,'C0001')
insert @t values(4,'D0001')
insert @t values(4,'D0002')
;with a as
(select code,
row_number() over (partition by code order by value) rna,
row_number() over (partition by code order by value desc) rnd,
value
from @t
), b as
(
select code, rnd, value, rna, cast(value as varchar(2000)) outvalue from a where rna = 1
union all
select a.code, a.rnd, a.value, a.rna, cast(b.outvalue || ',' || a.value as varchar(2000))
from a
join b on a.rna - 1 = b.rna and a.code = b.code
)
select code, outvalue from b
where rnd = 1
order by 1
--option (maxrecursion 0) -- not sure this is pl/sql
您正在尋找模擬'爲SQL Server GROUP_CONCAT'你會使用'XML PATH'在PLSQL –