안녕하세요 통계쿼리를 작성중에있습니다.
아래 쿼리는 기준년월에서부터 지난해 동월까지의
월별 통계쿼리입니다.
여기까지는 어찌어찌 하였는데 여기에 추가로 평균값을 같이 구하고 싶습니다..
아래쿼리를 실행하면 이런형식으로 데이터가 조회가 되는데요
17.1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월 '18.1월 16766 17797 22124 22918 26944 31696 31210 30885 22388 13802 19139 20678 20468
위 결과에 평균값을 아래와 같이 출력시키고 싶습니다..
조언부탁드립니다..
합 17.1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월 '18.1월 16766 17797 22124 22918 26944 31696 31210 30885 22388 13802 19139 20678 20468
평균 17.1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월 '18.1월 22833 22833 22833 22833 22833 22833 22833 22833 22833 22833 22833 22833 22833
select
''''||SUBSTR(mm01,3,2)||'.'||TO_NUMBER(SUBSTR(mm01,5,2))||'월' AS mm01,
DECODE(SUBSTR(mm02,5,2),'01', ''''||SUBSTR(mm02,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm02,5,2))||'월' AS mm02,
DECODE(SUBSTR(mm03,5,2),'01', ''''||SUBSTR(mm03,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm03,5,2))||'월' AS mm03,
DECODE(SUBSTR(mm04,5,2),'01', ''''||SUBSTR(mm04,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm04,5,2))||'월' AS mm04,
DECODE(SUBSTR(mm05,5,2),'01', ''''||SUBSTR(mm05,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm05,5,2))||'월' AS mm05,
DECODE(SUBSTR(mm06,5,2),'01', ''''||SUBSTR(mm06,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm06,5,2))||'월' AS mm06,
DECODE(SUBSTR(mm07,5,2),'01', ''''||SUBSTR(mm07,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm07,5,2))||'월' AS mm07,
DECODE(SUBSTR(mm08,5,2),'01', ''''||SUBSTR(mm08,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm08,5,2))||'월' AS mm08,
DECODE(SUBSTR(mm09,5,2),'01', ''''||SUBSTR(mm09,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm09,5,2))||'월' AS mm09,
DECODE(SUBSTR(mm10,5,2),'01', ''''||SUBSTR(mm10,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm10,5,2))||'월' AS mm10,
DECODE(SUBSTR(mm11,5,2),'01', ''''||SUBSTR(mm11,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm11,5,2))||'월' AS mm11,
DECODE(SUBSTR(mm12,5,2),'01', ''''||SUBSTR(mm12,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm12,5,2))||'월' AS mm12,
DECODE(SUBSTR(mm13,5,2),'01', ''''||SUBSTR(mm13,3,2)||'.', '')||TO_NUMBER(SUBSTR(mm13,5,2))||'월' AS mm13,
max(val01) as mm_val01,
max(val02) as mm_val02,
max(val03) as mm_val03,
max(val04) as mm_val04,
max(val05) as mm_val05,
max(val06) as mm_val06,
max(val07) as mm_val07,
max(val08) as mm_val08,
max(val09) as mm_val09,
max(val10) as mm_val10,
max(val11) as mm_val11,
max(val12) as mm_val12,
max(val13) as mm_val13
from(
SELECT
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-12), 'YYYYMM') AS mm01,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-11), 'YYYYMM') AS mm02,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-10), 'YYYYMM') AS mm03,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-9), 'YYYYMM') AS mm04,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-8), 'YYYYMM') AS mm05,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-7), 'YYYYMM') AS mm06,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-6), 'YYYYMM') AS mm07,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-5), 'YYYYMM') AS mm08,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-4), 'YYYYMM') AS mm09,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-3), 'YYYYMM') AS mm10,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-2), 'YYYYMM') AS mm11,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-1), 'YYYYMM') AS mm12,
TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), 0), 'YYYYMM') AS mm13,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-12), 'YYYYMM'), count(*)) AS val01,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-11), 'YYYYMM'), count(*)) AS val02,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-10), 'YYYYMM'), count(*)) AS val03,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -9), 'YYYYMM'), count(*)) AS val04,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -8), 'YYYYMM'), count(*)) AS val05,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -7), 'YYYYMM'), count(*)) AS val06,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -6), 'YYYYMM'), count(*)) AS val07,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -5), 'YYYYMM'), count(*)) AS val08,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -4), 'YYYYMM'), count(*)) AS val09,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -3), 'YYYYMM'), count(*)) AS val10,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -2), 'YYYYMM'), count(*)) AS val11,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), -1), 'YYYYMM'), count(*)) AS val12,
DECODE(SUBSTR(acc_ymd,1,6), TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'), 0), 'YYYYMM'), count(*)) AS val13
FROM tbl1
WHERE SUBSTR(acc_ymd,1,6) >= TO_CHAR(ADD_MONTHS(TO_DATE('201801','YYYYMM'),-12), 'YYYYMM') AND SUBSTR(acc_ymd,1,6) <= '201801'
GROUP BY SUBSTR (acc_ymd, 1, 6)
order by SUBSTR (acc_ymd, 1, 6)
)
group by mm01,mm02,mm03,mm04,mm05,mm06,mm07,mm08,mm09,mm10,mm11,mm12,mm13
|