--■■■instr関数を使う方法■■■
declare
WillOut Varchar2(200);
begin
for rec_Work in (select 'A10' as Val from dual
union all select 'B20' from dual
union all select 'C30A' from dual
union all select 'D40' from dual
union all select 'A10' from dual
union all select 'C30' from dual) Loop
if instr(',' || WillOut || ',',',' || rec_Work.Val || ',') = 0 then
WillOut := WillOut || ',' || rec_Work.Val;
end if;
end Loop;
DBMS_Output.Put_Line(substr(WillOut,2));
end;
/
--■■■正規表現を使う方法(10g以降)■■■
declare
WillOut Varchar2(200);
begin
for rec_Work in (select 'A10' as Val from dual
union all select 'B20' from dual
union all select 'C30A' from dual
union all select 'D40' from dual
union all select 'A10' from dual
union all select 'C30' from dual) Loop
if RegExp_Like(WillOut,'(^|,)' || rec_Work.Val || '($|,)') then
null;
else
WillOut := WillOut || ',' || rec_Work.Val;
end if;
end Loop;
DBMS_Output.Put_Line(substr(WillOut,2));
end;
/
--■■■model句を使う方法(10g以降)■■■
with WorkView as (select 'A10' as Val from dual
union all select 'B20' from dual
union all select 'C30A' from dual
union all select 'D40' from dual
union all select 'A10' from dual
union all select 'C30' from dual)
select max(Val) as Val
from (select Val
from WorkView
model
dimension by (RowNum as soeji)
measures(cast(Val as varchar2(200)) as Val)
rules(Val[any] order by soeji = case when cv(soeji) = 1 then Val[cv()]
when instr(',' || Val[cv()-1] || ',',',' || Val[cv()] || ',') > 0
then Val[cv()-1]
else Val[cv()-1] || ',' || Val[cv()] end));