안녕하세요. 현재년월을 기준으로 전년도 년월을 포함 사용자 날짜별 합계를 구현하고 있습니다.
올해 기준 2021년 9월06일부터 전년도 2020년 09월 07일까지의 사용자 날짜별 카운트 합계를 구하려고 아래와 같이
쿼리를 짰는데요.
문제는 현재년월을 기준의로 ADDDATE로 year, month를 이용해서 사용하다 보니 현재 월과 전년도 월에 따라서 적용이 되어야하는데 아래와 같이 현재 9월이니까 INTERVAL -0 MONTH 으로 되어 있습니다.
10월달이 되면 지금 쿼리로는 12개월을 넘어가서 다른 방법이 있는지 알려주시면 감사하겠습니다~.
전년도 월 :
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -0 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR9
현재 월 :
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -0 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR9
SELECT * FROM(
SELECT
ROWNUM RNUM, A.*
FROM
(SELECT
LOGIN_ID, LOGIN_NAME,
FORMAT(SUM(NVL(LAST_YEAR1, 0)+NVL(LAST_YEAR2, 0)+NVL(LAST_YEAR3, 0)+NVL(LAST_YEAR4, 0)+NVL(LAST_YEAR5, 0)+NVL(LAST_YEAR6, 0)+NVL(LAST_YEAR7, 0)+NVL(LAST_YEAR8, 0)+NVL(LAST_YEAR9, 0)+NVL(LAST_YEAR10, 0)+NVL(LAST_YEAR11, 0)+NVL(LAST_YEAR12, 0)+NVL(NOW_YEAR1, 0)+NVL(NOW_YEAR2, 0)+NVL(NOW_YEAR3, 0)+NVL(NOW_YEAR4, 0)+NVL(NOW_YEAR5, 0)+NVL(NOW_YEAR6, 0)+NVL(NOW_YEAR7, 0)+NVL(NOW_YEAR8, 0)+NVL(NOW_YEAR9, 0)+NVL(NOW_YEAR10, 0)+NVL(NOW_YEAR11, 0)+NVL(NOW_YEAR12, 0)), 0) TOTAL,
FORMAT(NVL(SUM(LAST_YEAR1), 0), 0) AS LAST_YEAR1,
FORMAT(NVL(SUM(LAST_YEAR2), 0), 0) AS LAST_YEAR2,
FORMAT(NVL(SUM(LAST_YEAR3), 0), 0) AS LAST_YEAR3,
FORMAT(NVL(SUM(LAST_YEAR4), 0), 0) AS LAST_YEAR4,
FORMAT(NVL(SUM(LAST_YEAR5), 0), 0) AS LAST_YEAR5,
FORMAT(NVL(SUM(LAST_YEAR6), 0), 0) AS LAST_YEAR6,
FORMAT(NVL(SUM(LAST_YEAR7), 0), 0) AS LAST_YEAR7,
FORMAT(NVL(SUM(LAST_YEAR8), 0), 0) AS LAST_YEAR8,
FORMAT(NVL(SUM(LAST_YEAR9), 0), 0) AS LAST_YEAR9,
FORMAT(NVL(SUM(LAST_YEAR10), 0), 0) AS LAST_YEAR10,
FORMAT(NVL(SUM(LAST_YEAR11), 0), 0) AS LAST_YEAR11,
FORMAT(NVL(SUM(LAST_YEAR12), 0), 0) AS LAST_YEAR12,
FORMAT(NVL(SUM(NOW_YEAR1), 0), 0) AS NOW_YEAR1,
FORMAT(NVL(SUM(NOW_YEAR2), 0), 0) AS NOW_YEAR2,
FORMAT(NVL(SUM(NOW_YEAR3), 0), 0) AS NOW_YEAR3,
FORMAT(NVL(SUM(NOW_YEAR4), 0), 0) AS NOW_YEAR4,
FORMAT(NVL(SUM(NOW_YEAR5), 0), 0) AS NOW_YEAR5,
FORMAT(NVL(SUM(NOW_YEAR6), 0), 0) AS NOW_YEAR6,
FORMAT(NVL(SUM(NOW_YEAR7), 0), 0) AS NOW_YEAR7,
FORMAT(NVL(SUM(NOW_YEAR8), 0), 0) AS NOW_YEAR8,
FORMAT(NVL(SUM(NOW_YEAR9), 0), 0) AS NOW_YEAR9,
FORMAT(NVL(SUM(NOW_YEAR10), 0), 0) AS NOW_YEAR10,
FORMAT(NVL(SUM(NOW_YEAR11), 0), 0) AS NOW_YEAR11,
FORMAT(NVL(SUM(NOW_YEAR12), 0), 0) AS NOW_YEAR12
FROM
(SELECT
LOGIN_ID, LOGIN_NAME,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -8 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR1,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -7 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR2,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -6 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR3,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -5 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR4,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -4 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR5,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -3 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR6,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -2 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR7,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -1 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR8,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -0 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR9,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL +1 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR10,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL +2 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR11,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL +3 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR12,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -8 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR1,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -7 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR2,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -6 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR3,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -5 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR4,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -4 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR5,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -3 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR6,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -2 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR7,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -1 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR8,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL -0 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR9,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL +1 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR10,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL +2 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR11,
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( SYSDATE , INTERVAL +3 MONTH), 'YYYYMM')) THEN COUNT(*) END NOW_YEAR12
FROM
TARGET_TABLE
WHERE
(TO_CHAR(CREATE_TIME, 'YYYYMMDD') BETWEEN (SELECT TO_CHAR(ADDDATE(ADDDATE( SYSDATE , INTERVAL +1 DAY), INTERVAL -1 YEAR), 'YYYYMMDD')) AND TO_CHAR(SYSDATE, 'YYYYMMDD'))
GROUP BY
LOGIN_ID, LOGIN_NAME, TO_CHAR(CREATE_TIME, 'YYYYMM'))
GROUP BY
LOGIN_ID, LOGIN_NAME) A);
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (SELECT TO_CHAR( ADDDATE( ADDDATE( SYSDATE , INTERVAL -0 MONTH), INTERVAL -1 YEAR), 'YYYYMM')) THEN COUNT(*) END LAST_YEAR9
부분을 그냥
CASE WHEN TO_CHAR(CREATE_TIME, 'YYYYMM') = (TO_CHAR(SYSDATE + (INTERVAL '-1' YEAR), 'YYYY') || '09') THEN COUNT(*) END LAST_YEAR9
로 하면 안되나요?
아..CUBRID는 좀 다르려나??
SELECT login_id , login_name , COUNT(*) total , COUNT(CASE ym WHEN last_year||'01' THEN 1 END) last_year01 , COUNT(CASE ym WHEN last_year||'02' THEN 1 END) last_year02 , COUNT(CASE ym WHEN last_year||'03' THEN 1 END) last_year03 , COUNT(CASE ym WHEN last_year||'04' THEN 1 END) last_year04 , COUNT(CASE ym WHEN last_year||'05' THEN 1 END) last_year05 , COUNT(CASE ym WHEN last_year||'06' THEN 1 END) last_year06 , COUNT(CASE ym WHEN last_year||'07' THEN 1 END) last_year07 , COUNT(CASE ym WHEN last_year||'08' THEN 1 END) last_year08 , COUNT(CASE ym WHEN last_year||'09' THEN 1 END) last_year09 , COUNT(CASE ym WHEN last_year||'10' THEN 1 END) last_year10 , COUNT(CASE ym WHEN last_year||'11' THEN 1 END) last_year11 , COUNT(CASE ym WHEN last_year||'12' THEN 1 END) last_year12 , COUNT(CASE ym WHEN now_year||'01' THEN 1 END) now_year01 , COUNT(CASE ym WHEN now_year||'02' THEN 1 END) now_year02 , COUNT(CASE ym WHEN now_year||'03' THEN 1 END) now_year03 , COUNT(CASE ym WHEN now_year||'04' THEN 1 END) now_year04 , COUNT(CASE ym WHEN now_year||'05' THEN 1 END) now_year05 , COUNT(CASE ym WHEN now_year||'06' THEN 1 END) now_year06 , COUNT(CASE ym WHEN now_year||'07' THEN 1 END) now_year07 , COUNT(CASE ym WHEN now_year||'08' THEN 1 END) now_year08 , COUNT(CASE ym WHEN now_year||'09' THEN 1 END) now_year09 , COUNT(CASE ym WHEN now_year||'10' THEN 1 END) now_year10 , COUNT(CASE ym WHEN now_year||'11' THEN 1 END) now_year11 , COUNT(CASE ym WHEN now_year||'12' THEN 1 END) now_year12 FROM (SELECT login_id , login_name , TO_CHAR(create_time, 'yyyymm') ym , TO_CHAR(sdt, 'yyyy') last_year , TO_CHAR(edt, 'yyyy') now_year FROM target_table a , (SELECT ADD_MONTHS(TRUNC(sysdate, 'dd'), -12) sdt -- 시작일 , TRUNC(sysdate, 'dd') edt -- 종료일 ) b WHERE create_time >= sdt + 1 AND create_time < edt + 1 ) c GROUP BY login_id, login_name ;