2017-03-04 4 views
0

모든 개를 위해 개 이외의 다른 동물의 이름을 찾아서 쉼표로 구분 된 목록에 추가하고 싶습니다. 테이블의 이미지 아래 쿼리의 원하는 결과 :listagg Oracle SQL 쿼리, 같은 테이블에 가입

테이블이 구조로 존재합니다 enter image description here

그리고 같은 결과 쿼리 만들 싶습니다 이 enter image description here

+0

읽어 보시기 바랍니다 http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload -images-of-code-on-so-ask-a-question/285557 및 허용 된 답변 –

답변

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 
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