集計機能collect()
を使用します。
ここにはOracle documentationへのリンクがあります。あなたのケースでは
、これは次のようになります。
create or replace type names_t as table of varchar2(50);
/
create or replace function join_names(names names_t)
return varchar2
as
ret varchar2(4000);
begin
for i in 1 .. names.count loop
if i > 1 then
ret := ret || ',';
end if;
ret := ret || names(i);
end loop;
return ret;
end join_names;
/
create table tq84_table (
id number,
seq number,
first_name varchar2(20),
last_name varchar2(20)
);
insert into tq84_table values (1, 1, 'John' , 'Walter');
insert into tq84_table values (1, 2, 'Michael', 'Jordan');
insert into tq84_table values (1, 3, 'Sally' , 'May' );
select
t1.id,
t1.seq,
join_names(
cast(collect(t2.first_name || ' ' || t2.last_name order by t2.seq)
as names_t)
)
from
tq84_table t1,
tq84_table t2
where
t1.id = t2.id and
t1.seq != t2.seq
group by t1.id, t1.seq
は、Oracle 11R2以上を使用している場合、あなたはまたを作成 を必要とせずにずっと簡単です LISTAGGを、(使用することができますタイプまたは機能):
クエリは、その後
select listagg(t2.first_name || ' ' || t2.last_name, ',')
within group (order by t2.seq)
over (partition by id) as names
from .... same as above ...
なり
与えられたIDに対して常に正確に3つのシーケンスIDがありますか? –
@Rene - いいえ、テーブルは任意の数のシーケンスを持つことができます! – jagamot