1. 문제의 쿼리
SELECT * FROM (SELECT 'D', -----Daily RSLT , COUNT(RSLT) FROM V_USER_STATS WHERE SU_USR_ID='2014106' AND SU_DT = TO_CHAR(SYSDATE , 'yyyymmdd') GROUP BY RSLT UNION SELECT 'm', -----Monthly RSLT , COUNT(RSLT) FROM V_USER_STATS WHERE SU_USR_ID='2014106' AND SU_DT LIKE TO_CHAR(SYSDATE , 'yyyymm')||'%' AND SU_DT <=TO_CHAR(SYSDATE , 'yyyymmdd') GROUP BY RSLT UNION SELECT 'y', -----Yearly RSLT , COUNT(RSLT) FROM V_USER_STATS WHERE SU_USR_ID='2014106' AND SU_DT LIKE TO_CHAR(SYSDATE , 'yyyy')||'%' AND SU_DT <=TO_CHAR(SYSDATE , 'yyyymmdd') GROUP BY RSLT ) |
결과
.. | rslt | count(rslt) |
d | 2 | 1 |
m | 1 | 3 |
m | 2 | 3 |
m | 3 | 2 |
y | 1 | 21 |
y | 2 | 5 |
y | 3 | 24 |
2. 하고 싶은것...
Daily , monthly , yearly .. 결과값(rslt) 통계를 내고 싶음 ㅜㅜ
추가로 하고 싶은것..
결과값은 무조건 1,2,3, 세개 인데..
갯수가 없는 것도 0으로 출력하고 싶고 ㅜㅜ
Ex)
rslt | count(rslt) | |
d | 1 | 0 |
d | 2 | 1 |
d | 3 | 0 |
3. 아무리 봐도 저 쿼리가 비효율적인거 같네요.ㅠㅠ
사실 .. 저 결과 테이블을 화면에 뿌려야 하는데... 어떻게 담아서 뿌려줘야 할지 모르겠어요ㅠㅠ
SELECT gb, rslt, cnt FROM (SELECT rslt , COUNT(DECODE(su_dt, TO_CHAR(sysdate, 'yyyymmdd'), 1)) d , COUNT(DECODE(SUBSTR(su_dt, 1, 6), TO_CHAR(sysdate, 'yyyymm'), 1)) m , COUNT(*) y FROM v_user_stats WHERE su_usr_id = '2014106' AND su_dt >= TO_CHAR(sysdate, 'yyyy')||'0101' AND su_dt <= TO_CHAR(sysdate, 'yyyymmdd') GROUP BY rslt ) UNPIVOT (cnt FOR gb IN (d, m, y)) ORDER BY gb, rslt ;
SELECT gb , rslt , DECODE(gb, 'd', d, 'm', m, 'y', y) cnt FROM (SELECT rslt , COUNT(DECODE(su_dt, TO_CHAR(sysdate, 'yyyymmdd'), 1)) d , COUNT(DECODE(SUBSTR(su_dt, 1, 6), TO_CHAR(sysdate, 'yyyymm'), 1)) m , COUNT(*) y FROM v_user_stats WHERE su_usr_id = '2014106' AND su_dt >= TO_CHAR(sysdate, 'yyyy')||'0101' AND su_dt <= TO_CHAR(sysdate, 'yyyymmdd') GROUP BY rslt ) , (SELECT 'd' gb FROM dual UNION ALL SELECT 'm' FROM dual UNION ALL SELECT 'y' FROM dual ) ORDER BY gb, rslt ;