select a.rescentarea, min(d.managename) 구분, count(*) 총계, sum(case when b.sexdiv = 'M' then 1 else 0 end) 남계, sum(case when b.sexdiv = 'F' then 1 else 0 end) 여계, sum(case when fngetage(b.respsno) < 20 then 1 else 0 end) age10대이하_계, sum(case when fngetage(b.respsno) < 20 and b.sexdiv = 'M' then 1 else 0 end) age10대이하_남, sum(case when fngetage(b.respsno) < 20 and b.sexdiv = 'F' then 1 else 0 end) age10대이하_여, sum(case when fngetage(b.respsno) between 20 and 29 then 1 else 0 end) age20대_계, sum(case when fngetage(b.respsno) between 20 and 29 and b.sexdiv = 'M' then 1 else 0 end) age20대_남, sum(case when fngetage(b.respsno) between 20 and 29 and b.sexdiv = 'F' then 1 else 0 end) age20대_여, sum(case when fngetage(b.respsno) between 30 and 39 then 1 else 0 end) age30대_계, sum(case when fngetage(b.respsno) between 30 and 39 and b.sexdiv = 'M' then 1 else 0 end) age30대_남, sum(case when fngetage(b.respsno) between 30 and 39 and b.sexdiv = 'F' then 1 else 0 end) age30대_여, sum(case when fngetage(b.respsno) between 40 and 49 then 1 else 0 end) age40대_계, sum(case when fngetage(b.respsno) between 40 and 49 and b.sexdiv = 'M' then 1 else 0 end) age40대_남, sum(case when fngetage(b.respsno) between 40 and 49 and b.sexdiv = 'F' then 1 else 0 end) age40대_여, sum(case when fngetage(b.respsno) between 50 and 59 then 1 else 0 end) age50대_계, sum(case when fngetage(b.respsno) between 50 and 59 and b.sexdiv = 'M' then 1 else 0 end) age50대_남, sum(case when fngetage(b.respsno) between 50 and 59 and b.sexdiv = 'F' then 1 else 0 end) age50대_여, sum(case when fngetage(b.respsno) >= 60 then 1 else 0 end) age60대_계, sum(case when fngetage(b.respsno) >= 60 and b.sexdiv = 'M' then 1 else 0 end) age60대_남, sum(case when fngetage(b.respsno) >= 60 and b.sexdiv = 'F' then 1 else 0 end) age60대_여 from (select resno, rescentarea from resactres_2014 group by resno, rescentarea) a, resmst_2014 b, managecode d where a.resno = b.resno and a.rescentarea = d.managecode and d.largecode = 'CM00' group by a.rescentarea order by rescentarea
이 부분이 기본 코딩이고
그 결과 내용이
구분 ㅣ 총계 ㅣ 남 ㅣ 여
계 123 50 68
서울 90 30 20
부산 33 10 15
이런식인데... 위의 뿌려주는 단의 쿼리는 같은데
(Select distinct resno from resactres_blood_2014 minus select distinct resno from resactres_2014)
이 새로운 쿼리가 들어가면
구분 ㅣ 총계 ㅣ 남 ㅣ 여
계 143 50 68
중앙 20 5 9
서울 90 30 20
부산 33 10 15
이런식으로 나오는 거죠;;;
근데 중요한건... 구분 쪽 컬럼에서 중앙이라는 데이터가 없어서
'중앙' 을 넣어야 하는데;;; 이게 참... 코드 값으로 불러오는 건데...
어떻게 중앙을 넣어야 할지도 모르겠습니다.
흐으음... 쿼리를 2개 돌려서 붙이면 되는 부분이기는 한데
궁금해서... 혹시나 하고 문의 드립니다 ㅠㅠ
바쁘신 와중에 글 읽어주셔서 감사합니다~
SELECT NVL(구분, '계') 구분 , COUNT(*) 총계 , COUNT(DECODE( sex, 'M', 1)) 남계 , COUNT(DECODE( sex, 'F', 1)) 여계 , COUNT(DECODE(age , '1' , 1)) age10대이하_계 , COUNT(DECODE(age||sex, '1M', 1)) age10대이하_남 , COUNT(DECODE(age||sex, '1F', 1)) age10대이하_여 , COUNT(DECODE(age , '2' , 1)) age20대_계 , COUNT(DECODE(age||sex, '2M', 1)) age20대_남 , COUNT(DECODE(age||sex, '2F', 1)) age20대_여 , COUNT(DECODE(age , '3' , 1)) age30대_계 , COUNT(DECODE(age||sex, '3M', 1)) age30대_남 , COUNT(DECODE(age||sex, '3F', 1)) age30대_여 , COUNT(DECODE(age , '4' , 1)) age40대_계 , COUNT(DECODE(age||sex, '4M', 1)) age40대_남 , COUNT(DECODE(age||sex, '4F', 1)) age40대_여 , COUNT(DECODE(age , '5' , 1)) age50대_계 , COUNT(DECODE(age||sex, '5M', 1)) age50대_남 , COUNT(DECODE(age||sex, '5F', 1)) age50대_여 , COUNT(DECODE(age , '6' , 1)) age60대이상_계 , COUNT(DECODE(age||sex, '6M', 1)) age60대이상_남 , COUNT(DECODE(age||sex, '6F', 1)) age60대이상_여 FROM (SELECT a.rescentarea , NVL(d.managename, '중앙') 구분 , b.sexdiv sex , LEAST(GREATEST(TRUNC(fngetage(b.respsno)/10), 1), 6) age , ROWNUM rn -- 뷰머징 방지용 -- FROM (SELECT DISTINCT rescentarea, resno FROM resactres_2014 WHERE a.rescentarea != '총계코드값' UNION ALL SELECT ' ' rescentarea, a.resno FROM (SELECT DISTINCT resno FROM resactres_blood_2014) a LEFT OUTER JOIN resactres_2014 b ON a.resno = b.resno WHERE b.resno IS NULL ) a , resmst_2014 b , managecode d WHERE a.resno = b.resno AND a.rescentarea = d.managecode(+) AND d.largecode(+) = 'CM00' ) x GROUP BY ROLLUP((rescentarea, 구분)) ORDER BY GROUPING(x.rescentarea) DESC, x.rescentarea ;