DATABASE

[ORACLE] LISTAGG함수

오래오래 늘 함께 2018. 11. 6. 15:26
  • LISTAGG( 컬럼A, 구분자 ) WITHIN GROUP ( ORDER BY 컬럼B)
: 컬럼A의 값들을 [구분자]와 같이 컬럼B의 기준으로 정렬하여 한 행으로 보여주는 함수로 


  • 예시
select * 
from APPART
where apgrade='907'



1. 각 원별로 학과 보여주기 


select apdiv, 
  listagg( apdept, ' ; ' ) within group (order by apno )
from APPART
where apgrade='907'
group by apdiv


select apdiv, 
  listagg( apdept' ; ' ) within group (order by apno desc --- apno의 내림차순으로
from APPART
where apgrade='907'
group by apdiv




2. 각 원별로 중복포함하여 학과 보여주기


select apdiv, 

  listagg(apdept, ' ; ' ) within group (order by apno ) over (partition by apdiv ) --- apdiv의 중복컬럼조회

fromAPPART

where apgrade='907'

group by apdiv