SELECT 구분 ,SUM(tot) AS tot ,SUM(시봉) AS 시봉 ,SUM(재봉) AS 재봉 ,SUM(전봉) AS 전봉 ,SUM(지사) AS 지사 ,SUM(헌) AS 헌 ,SUM(기) AS 기 FROM( SELECT CASE WHEN AGE < 20 AND SEXDIV = 'F' THEN '10대이하여자' WHEN AGE < 20 AND SEXDIV = 'M' THEN '10대이하남자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'F' THEN '20대여자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'M' THEN '20대남자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'F' THEN '30대여자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'M' THEN '30대남자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'F' THEN '40대여자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'M' THEN '40대남자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'F' THEN '50대여자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'M' THEN '50대남자' WHEN AGE >= 60 AND SEXDIV = 'F' THEN '60대이상여자' WHEN AGE >= 60 AND SEXDIV = 'M' THEN '60대이상남자' END AS 구분 ,tot ,시봉 ,재봉 ,전봉 ,지사 ,헌 ,기 FROM( SELECT FNGETAGE(b.RESPSNO) AS AGE ,b.SEXDIV ,COUNT(*) AS tot, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '01' THEN 1 ELSE 0 END) 시봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '02' THEN 1 ELSE 0 END) 재봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '03' THEN 1 ELSE 0 END) 전봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '04' THEN 1 ELSE 0 END) 지사, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '07' THEN 1 ELSE 0 END) 헌, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) NOT IN ('01','02','03','04','07') OR A.ACTTYPE IS NULL THEN 1 ELSE 0 END) 기 FROM RESACTRES_2014 A,RESMST_2014 B WHERE A.RESNO = B.RESNO GROUP BY FNGETAGE(b.RESPSNO), b.SEXDIV ) ) GROUP BY 구분 union SELECT 구분 ,SUM(tot) AS tot ,SUM(시봉) AS 시봉 ,SUM(재봉) AS 재봉 ,SUM(전봉) AS 전봉 ,SUM(지사) AS 지사 ,SUM(헌) AS 헌 ,SUM(기) AS 기 FROM( SELECT CASE WHEN AGE < 20 AND SEXDIV = 'F' THEN '10대이하여자' WHEN AGE < 20 AND SEXDIV = 'M' THEN '10대이하남자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'F' THEN '20대여자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'M' THEN '20대남자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'F' THEN '30대여자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'M' THEN '30대남자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'F' THEN '40대여자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'M' THEN '40대남자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'F' THEN '50대여자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'M' THEN '50대남자' WHEN AGE >= 60 AND SEXDIV = 'F' THEN '60대이상여자' WHEN AGE >= 60 AND SEXDIV = 'M' THEN '60대이상남자' END AS 구분 ,tot ,시봉 ,재봉 ,전봉 ,지사 ,헌 ,기 FROM( SELECT FNGETAGE(b.RESPSNO) AS AGE ,b.SEXDIV ,COUNT(*) AS tot, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '01' THEN 1 ELSE 0 END) 시봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '02' THEN 1 ELSE 0 END) 재봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '03' THEN 1 ELSE 0 END) 전봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '04' THEN 1 ELSE 0 END) 지사, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '07' THEN 1 ELSE 0 END) 헌, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) NOT IN ('01','02','03','04','07') OR A.ACTTYPE IS NULL THEN 1 ELSE 0 END) 기 FROM resactres_blood_2014 A,RESMST_2014 B WHERE A.RESNO = B.RESNO(+) GROUP BY FNGETAGE(b.RESPSNO), b.SEXDIV ) ) GROUP BY 구분 ORDER BY 구분
union 위의 쿼리의 경우 헌 컬럼이 빈 데이터가 나오고
union 밑에 있는 쿼리의 경우 헌 컬럼의 합이 422,341의 값이 나와야 합니다.
그런데...
값이 절반 밖에 안나오더라구요 ㅠㅠ
하아... 그래서 소스를 쪼개서 보다보니...
밑에 있는 소스는 정상적으로 보이는데 ... group by 구분 쪽에서 데이터 distinct 값이 들어가서 그런거 같습니다.
물론... union도 distinct를 하긴 하지만...
밑에 있는 데이터가 distinct값이 안걸리게 잘 나오는 방법은 없을까요?...
SELECT CASE WHEN AGE < 20 AND SEXDIV = 'F' THEN '10대이하여자' WHEN AGE < 20 AND SEXDIV = 'M' THEN '10대이하남자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'F' THEN '20대여자' WHEN AGE BETWEEN 20 AND 29 AND SEXDIV = 'M' THEN '20대남자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'F' THEN '30대여자' WHEN AGE BETWEEN 30 AND 39 AND SEXDIV = 'M' THEN '30대남자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'F' THEN '40대여자' WHEN AGE BETWEEN 40 AND 49 AND SEXDIV = 'M' THEN '40대남자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'F' THEN '50대여자' WHEN AGE BETWEEN 50 AND 59 AND SEXDIV = 'M' THEN '50대남자' WHEN AGE >= 60 AND SEXDIV = 'F' THEN '60대이상여자' WHEN AGE >= 60 AND SEXDIV = 'M' THEN '60대이상남자' END AS 구분 ,tot ,시봉 ,재봉 ,전봉 ,지사 ,헌 ,기 FROM( SELECT FNGETAGE(b.RESPSNO) AS AGE ,b.SEXDIV ,COUNT(*) AS tot, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '01' THEN 1 ELSE 0 END) 시봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '02' THEN 1 ELSE 0 END) 재봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '03' THEN 1 ELSE 0 END) 전봉, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '04' THEN 1 ELSE 0 END) 지사, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) = '07' THEN 1 ELSE 0 END) 헌, SUM(CASE WHEN SUBSTR(A.ACTTYPE,1,2) NOT IN ('01','02','03','04','07') OR A.ACTTYPE IS NULL THEN 1 ELSE 0 END) 기 FROM resactres_blood_2014 A,RESMST_2014 B WHERE A.RESNO = B.RESNO(+) GROUP BY FNGETAGE(b.RESPSNO), b.SEXDIV )