어찌하여 쿼리는 완성하였는데 효율적인 방법이 또 있는지 다시 질문드리겠습니다..
with t as
(
select '201610' as 년도, '123' as 지역, 54 as 개방문, 65 as 고양이방문, 24 as 오전방문,74 as 오후방문 from dual union all
select '201611' as 년도, '123' as 지역, 54 as 개방문, 65 as 고양이방문, 24 as 오전방문,74 as 오후방문 from dual union all
select '201611' as 년도, '234' as 지역, 23 as 개방문, 13 as 고양이방문, 53 as 오전방문,34 as 오후방문 from from dual union all
select '201611' as 년도, '534' as 지역, 53 as 개방문, 6 as 고양이방문, 3 as 오전방문,5 as 오후방문 from from dual union all
select '201611' as 년도, '234' as 지역, 12 as 개방문, 3 as 고양이방문, 123 as 오전방문,35 as 오후방문 from from dual union all
select '201611' as 년도, '153' as 지역, 53 as 개방문, 74 as 고양이방문, 52 as 오전방문,63 as 오후방문 from from dual union all
select '201701' as 년도, '234' as 지역, 123 as 개방문, 14 as 고양이방문, 96 as 오전방문,34 as 오후방문 from from dual union all
select '201702' as 년도, '125' as 지역, 634 as 개방문, 4 as 고양이방문, 45 as 오전방문,63 as 오후방문 from from dual union all
select '201703' as 년도, '125' as 지역, 12 as 개방문, 104 as 고양이방문, 35 as 오전방문,35 as 오후방문 from from dual union all
select '201703' as 년도, '125' as 지역, 93 as 개방문, 23 as 고양이방문, 75 as 오전방문,63 as 오후방문 from from dual union all
select '201709' as 년도, '123' as 지역, 231 as 개방문, 53 as 고양이방문, 12 as 오전방문,63 as 오후방문 from from dual union all
select '201709' as 년도, '125' as 지역, 321 as 개방문, 1 as 고양이방문, 64 as 오전방문,73 as 오후방문 from from dual union all
select '201711' as 년도, '153' as 지역, 85 as 개방문, 53 as 고양이방문, 34 as 오전방문,12 as 오후방문 from from dual union all
select '201711' as 년도, '123' as 지역, 32 as 개방문, 74 as 고양이방문, 63 as 오전방문,123 as 오후방문 from from dual union all
select '201711' as 년도, '234' as 지역, 75 as 개방문, 85 as 고양이방문, 112 as 오전방문,163 as 오후방문 from from dual union all
select '201711' as 년도, '008' as 지역, 53 as 개방문, 4 as 고양이방문, 3 as 오전방문,26 as 오후방문 from from dual
)
result row :
개증감구분 | 개 증감율 | 고양이증감구분 | 고양이 증감율 | 증감률 높은거(ex 개 or 고양이) | am 증감율 | pm 증감율 | 증감율 높은거(ex 오전 or 오후) |
+ | + 30.3% | - | - 15.4% | 개 | -83.2% | + 104.2% | 오후 |
결과 로우는 한줄로 저렇게 나오길 원합니다.
조건은 선택된 지역, 작년 동월 대비 방문횟수 증감률입니다. (ex. 지역 in ( '153', '234', '153') )
( 선택된 지역의 201611월 방문율과 201711월 방문율 데이터 비교하여 증감률 )
WITH t AS ( SELECT '201610' 년도, '123' 지역, 54 개방문, 65 고양이방문, 24 오전방문, 74 오후방문 FROM dual UNION ALL SELECT '201611', '123', 54, 65, 24, 74 FROM dual UNION ALL SELECT '201611', '234', 23, 13, 53, 34 FROM dual UNION ALL SELECT '201611', '534', 53, 6, 3, 5 FROM dual UNION ALL SELECT '201611', '234', 12, 3, 123, 35 FROM dual UNION ALL SELECT '201611', '153', 53, 74, 52, 63 FROM dual UNION ALL SELECT '201701', '234', 123, 14, 96, 34 FROM dual UNION ALL SELECT '201702', '125', 634, 4, 45, 63 FROM dual UNION ALL SELECT '201703', '125', 12, 104, 35, 35 FROM dual UNION ALL SELECT '201703', '125', 93, 23, 75, 63 FROM dual UNION ALL SELECT '201709', '123', 231, 53, 12, 63 FROM dual UNION ALL SELECT '201709', '125', 321, 1, 64, 73 FROM dual UNION ALL SELECT '201711', '153', 85, 53, 34, 12 FROM dual UNION ALL SELECT '201711', '123', 32, 74, 63, 123 FROM dual UNION ALL SELECT '201711', '234', 75, 85, 112, 163 FROM dual UNION ALL SELECT '201711', '008', 53, 4, 3, 26 FROM dual ) SELECT DECODE(SIGN(개2-개1), 1, '+', -1, '-', 0, '0', 'new') 개증감구분 , ROUND((개2-개1)/개1*100, 2) 개증감 , DECODE(SIGN(냥2-냥1), 1, '+', -1, '-', 0, '0', 'new') 냥증감구분 , ROUND((냥2-냥1)/냥1*100, 2) 냥증감 , CASE WHEN (개2-개1)/개1 > (냥2-냥1)/냥1 THEN '개' WHEN (개2-개1)/개1 < (냥2-냥1)/냥1 THEN '냥' WHEN (개2-개1)/개1 = (냥2-냥1)/냥1 THEN '개냥_동률' END 증감높은거_개냥 , ROUND((am2-am1)/am1*100, 2) am증감 , ROUND((pm2-pm1)/pm1*100, 2) pm증감 , CASE WHEN (am2-am1)/am1 > (pm2-pm1)/pm1 THEN 'AM' WHEN (am2-am1)/am1 < (pm2-pm1)/pm1 THEN 'PM' WHEN (am2-am1)/am1 = (pm2-pm1)/pm1 THEN 'AM_PM_동률' END 증감높은거_AM_PM FROM (SELECT SUM(DECODE(년도, :v_ym, null, 개방문 )) 개1 , SUM(DECODE(년도, :v_ym, 개방문, 0)) 개2 , SUM(DECODE(년도, :v_ym, null, 고양이방문 )) 냥1 , SUM(DECODE(년도, :v_ym, 고양이방문, 0)) 냥2 , SUM(DECODE(년도, :v_ym, null, 오전방문 )) am1 , SUM(DECODE(년도, :v_ym, 오전방문, 0)) am2 , SUM(DECODE(년도, :v_ym, null, 오후방문 )) pm1 , SUM(DECODE(년도, :v_ym, 오후방문, 0)) pm2 FROM t WHERE 지역 IN (:v1, :v2, :v3) -- ('153', '234', '123') AND 년도 IN (:v_ym, TO_CHAR(:v_ym - 100)) -- ('201711') ) ;