-테이블
dept_code(부서코드 | id (아이디) | salary (연봉) |
001 | ID1 | 3000 |
001 | ID2 | 4000 |
002 | ID3 | 3500 |
003 | ID4 | 4000 |
001, 002, 003 각각 부서의 연봉 합계를 구하는것이 아닌
001, 002+003 부서의 연봉 합계를 구하는 방법이 궁급합니다.
(001부서의 연봉 합계, (002, 003)의 부서의 연봉 합계)
-결과 테이블
dept (부서) | salary (연봉합계) |
001 | 7000 |
002+003(기타부서) | 7500 |
매번 도와주셔서 감사합니다.
with t as ( select '001' as dept_code, 'ID1' as id, 3000 as salary from dual union all select '001' as dept_code, 'ID2' as id, 4000 as salary from dual union all select '002' as dept_code, 'ID3' as id, 3500 as salary from dual union all select '003' as dept_code, 'ID4' as id, 4000 as salary from dual ) select decode(dept_code,'001','001','기타부서') as dept , sum(salary) as sal from t group by decode(dept_code,'001','001','기타부서') order by 1
예시 출력형식대로..
with t as ( select '001' as dept_code, 'ID1' as id, 3000 as salary from dual union all select '001' as dept_code, 'ID2' as id, 4000 as salary from dual union all select '002' as dept_code, 'ID3' as id, 3500 as salary from dual union all select '003' as dept_code, 'ID4' as id, 4000 as salary from dual ) select regexp_replace(listagg(dept_code,'+') within group (order by dept_code),'([^+]+)(\+\1)+','\1') || decode(dept_code,'001',null,'(기타부서)') as dept , sum(salary) as sal from t group by decode(dept_code,'001',null,'(기타부서)') order by 1