答案 0 :(得分:0)
您可以使用self-join
,然后使用listagg
。
select tdog.animal,tdog.name
,listagg(tother.animal||'-'||tother.name||'-'||tother.id) within group(order by tother.id)
from tablename tdog
join tablename tother on tdog.name=tother.name and tdog.animal='dog' and tother.animal <> 'dog'
group by tdog.animal,tdog.name
答案 1 :(得分:0)
通过一些技巧,您不需要self join
:
select 'dog' as animal, name,
listagg(case when animal <> 'dog' then animal || '-' || name || '-' || id
end), ',') within group (order by id) as animals
from t
group by name
having sum(case when animal = 'dog' then 1 else 0 end) > 0