【发布时间】:2017-03-04 01:04:29
【问题描述】:
【问题讨论】:
【问题讨论】:
您可以使用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
【讨论】:
有了一些技巧,你就不需要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
【讨论】: