안녕하세요.
연령별 통계를 구하는 쿼리인데
값이 있는 연령대별만 노출이 됩니다.
해당 연령대에 값이 없는경우도 노출을 할 수 있을까요?
SELECT NVL2(age_gb, age_gb, '계') AS age_gb , SUM(CASE WHEN 등록연도 = '2020' THEN 횟수 ELSE 0 END) y2020 , SUM(CASE WHEN 등록연도 = '2021' THEN 횟수 ELSE 0 END) y2021 , SUM(CASE WHEN 등록연도 = '2022' THEN 횟수 ELSE 0 END) y2022 , SUM(CASE WHEN 등록연도 = '2023' THEN 횟수 ELSE 0 END) y2023 FROM (SELECT CASE WHEN age < 20 THEN '20미만' WHEN age >= 20 AND age < 30 THEN '20-29' WHEN age >= 30 AND age < 40 THEN '30-39' WHEN age >= 40 AND age < 50 THEN '40-49' WHEN age >= 50 AND age < 60 THEN '50-59' WHEN age >= 60 AND age < 70 THEN '60-69' WHEN age >= 70 THEN '70이상' END age_gb, 등록연도, 횟수 FROM (SELECT 등록연도, 횟수, 연령 AS age FROM 등록테이블 ) ) GROUP BY ROLLUP(age_gb, 등록연도) HAVING GROUPING_ID(age_gb, 등록연도) IN (0, 3) ORDER BY age_gb, 등록연도
SELECT NVL(a.age_gb, '계') age_gb , SUM(DECODE(b.등록연도, '2020', b.횟수, 0)) y2020 , SUM(DECODE(b.등록연도, '2021', b.횟수, 0)) y2021 , SUM(DECODE(b.등록연도, '2022', b.횟수, 0)) y2022 , SUM(DECODE(b.등록연도, '2023', b.횟수, 0)) y2023 FROM (SELECT 0 s, 20 e, '20미만' age_gb FROM dual UNION ALL SELECT 20, 30, '20-29' FROM dual UNION ALL SELECT 30, 40, '30-39' FROM dual UNION ALL SELECT 40, 50, '40-49' FROM dual UNION ALL SELECT 50, 60, '50-59' FROM dual UNION ALL SELECT 60, 70, '60-69' FROM dual UNION ALL SELECT 70, 999, '70이상' FROM dual ) a LEFT OUTER JOIN (SELECT 등록연도, 횟수, 연령 age FROM 등록테이블 WHERE 등록연도 BETWEEN '2020' AND '2023' ) b ON c.age >= a.s AND c.age < a.e GROUP BY ROLLUP((a.s, a.age_gb)) ;