일차적으로 조회해온 테이블 입니다..
요기까지 짜는데도 많은 수행착오가 있었네요 흑..
이 테이블을 근간으로 통계적으로 보여줘야 하는데..
dept_id | type | cnt | name | no |
DEPT1 | 1 | 1 | 김 | 4730 |
DEPT1 | 0 | 1 | 김 | 4731 |
DEPT2 | 0 | 1 | 이 | 4016 |
DEPT3 | 3 | 1 | 박 | 4573 |
DEPT4 | 0 | 1 | 윤 | 4048 |
DEPT5 | 2 | 4 | 황,박,김,박 | 4289, 4383, 4488, 4603 |
DEPT5 | 3 | 3 | 황,이,김 | 4516, 4525, 4536 |
DEPT5 | 0 | 1 | 박 | 4522 |
DEPT7 | 3 | 1 | 김 | 4398 |
DEPT7 | 0 | 1 | 김 | 4397 |
DEPT9 | 0 | 1 | 최 | 4635 |
DEPT26 | 2 | 2 | 이,정 | 4287, 4288 |
DEPT26 | 0 | 1 | 성 | 4024 |
위의 테이블을 가공해서 dept_id 로 그룹바이 되고 type 열 그룹바이 해서.. 각 타입의 종류갯수를 세어냄..
설명도 이상하네요 ㅠㅠ
type1_cnt | type1_name | type1_no | type2_cnt | type2_name | type2_no | type3_cnt | type3_name | type3_no | type0_cnt | type0_name | type0_no | |
DEPT1 | 1 | 김 | 4730 | 1 | 김 | 4731 | ||||||
DEPT2 | 1 | 이 | 4016 | |||||||||
DEPT3 | 1 | 박 | 4573 | |||||||||
DEPT4 | 1 | 윤 | 4048 | |||||||||
DEPT5 | 4 | 황,박,김,박 | 4289, 4383, 4488, 4603 | 3 | 황,이,김 | 4516, 4525, 4536 | 1 | 박 | 4522 | |||
DEPT7 | 1 | 김 | 4398 | 1 | 김 | 4397 | ||||||
DEPT9 | 1 | 최 | 4635 | |||||||||
DEPT26 | 2 | 이,정 | 4287, 4288 | 1 | 성 | 4024 |
이해가 되실지 모르겠지만.. 세로줄은 deptid로 그룹이 되야 하고.. 가로줄에.. type 별 항목들의 값들이 나와야 합니다..
select dept_id , type1,type2 ,type3 , type4
from(
)
pivot(max(cnt) for type in(1 as type1, 2 as type, 3 as type3, 4 as type4, 0 as type5)
식으로 하면 dept_id 로 그룹핑이 안되고 ㅜㅜ 나머지 값들(name과 no 항목을 가져올수가 없네요..)
먹고사는 밥줄인데 이리 힘드니 ㅜㅜ 창피할 따름입니다..
단지 화면 조작만 하면 되는줄 알았는데 이런 복잡한 쿼리도 짜야하니.. 한계를 느끼고 있어요..
--GROUP BY 이용 WITH T(dept_id, type, cnt, name, no) AS ( SELECT 'DEPT1', '1', '1', '김', '4730' FROM DUAL UNION ALL SELECT 'DEPT1', '0', '1', '김', '4731' FROM DUAL UNION ALL SELECT 'DEPT2', '0', '1', '이', '4016' FROM DUAL UNION ALL SELECT 'DEPT3', '3', '1', '박', '4573' FROM DUAL UNION ALL SELECT 'DEPT4', '0', '1', '윤', '4048' FROM DUAL UNION ALL SELECT 'DEPT5', '2', '4', '황,박,김,박', '4289, 4383, 4488, 4603' FROM DUAL UNION ALL SELECT 'DEPT5', '3', '3', '황,이,김', '4516, 4525, 4536' FROM DUAL UNION ALL SELECT 'DEPT5', '0', '1', '박', '4522' FROM DUAL UNION ALL SELECT 'DEPT7', '3', '1', '김', '4398' FROM DUAL UNION ALL SELECT 'DEPT7', '0', '1', '김', '4397' FROM DUAL UNION ALL SELECT 'DEPT9', '0', '1', '최', '4635' FROM DUAL UNION ALL SELECT 'DEPT26', '2', '2', '이,정', '4287, 4288' FROM DUAL UNION ALL SELECT 'DEPT26', '0', '1', '성', '4024' FROM DUAL ) SELECT dept_id, MAX(DECODE(type, 1, cnt)) AS type1_cnt, MAX(DECODE(type, 1, name)) AS type1_name, MAX(DECODE(type, 1, no)) AS type1_no, MAX(DECODE(type, 2, cnt)) AS type2_cnt, MAX(DECODE(type, 2, name)) AS type2_name, MAX(DECODE(type, 2, no)) AS type2_no, MAX(DECODE(type, 3, cnt)) AS type3_cnt, MAX(DECODE(type, 3, name)) AS type3_name, MAX(DECODE(type, 3, no)) AS type3_no, MAX(DECODE(type, 0, cnt)) AS type0_cnt, MAX(DECODE(type, 0, name)) AS type0_name, MAX(DECODE(type, 0, no)) AS type0_no FROM T GROUP BY dept_id ORDER BY TO_NUMBER(REPLACE(dept_id, 'DEPT', ''))
WITH TT(dept_id, type, cnt, name, no) AS ( SELECT 'DEPT1', 1, 1, '김', '4730' FROM DUAL UNION ALL SELECT 'DEPT1', 0, 1, '김', '4731' FROM DUAL UNION ALL SELECT 'DEPT2', 0, 1, '이', '4016' FROM DUAL UNION ALL SELECT 'DEPT3', 3, 1, '박', '4573' FROM DUAL UNION ALL SELECT 'DEPT4', 0, 1, '윤', '4048' FROM DUAL UNION ALL SELECT 'DEPT5', 2, 4, '황,박,김,박', '4289, 4383, 4488, 4603' FROM DUAL UNION ALL SELECT 'DEPT5', 3, 3, '황,이,김', '4516, 4525, 4536' FROM DUAL UNION ALL SELECT 'DEPT5', 0, 1, '박', '4522' FROM DUAL UNION ALL SELECT 'DEPT7', 3, 1, '김', '4398' FROM DUAL UNION ALL SELECT 'DEPT7', 0, 1, '김', '4397' FROM DUAL UNION ALL SELECT 'DEPT9', 0, 1, '최', '4635' FROM DUAL UNION ALL SELECT 'DEPT26', 2, 2, '이,정', '4287, 4288' FROM DUAL UNION ALL SELECT 'DEPT26', 0, 1, '성', '4024' FROM DUAL ) SELECT * FROM TT PIVOT ( MAX(CNT) AS TYPE_CNT , MAX(NAME) AS TYPE_NAME , MAX(NO) AS TYPE_NO FOR TYPE IN (1,2,3,0) ) ORDER BY TO_NUMBER(SUBSTR(DEPT_ID,5)) ;
SELECT DEPT_ID , "1_TYPE_CNT" AS TYPE1_CNT, "1_TYPE_NAME" AS TYPE1_NAME, "1_TYPE_NO" AS TYPE1_NO , "2_TYPE_CNT" AS TYPE2_CNT, "2_TYPE_NAME" AS TYPE2_NAME, "2_TYPE_NO" AS TYPE2_NO , "3_TYPE_CNT" AS TYPE3_CNT, "3_TYPE_NAME" AS TYPE3_NAME, "3_TYPE_NO" AS TYPE3_NO , "0_TYPE_CNT" AS TYPE0_CNT, "0_TYPE_NAME" AS TYPE0_NAME, "0_TYPE_NO" AS TYPE0_NO FROM TT PIVOT ( MAX(CNT) AS TYPE_CNT , MAX(NAME) AS TYPE_NAME , MAX(NO) AS TYPE_NO FOR TYPE IN (1,2,3,0) ) ORDER BY TO_NUMBER(SUBSTR(DEPT_ID,5)) ;