안녕하세요 데이터베이스 공부하는 초보인데요 PLSQL 작성하다가 궁금한 부분이 있어서 질문드립니다. 1) 특정 월(ex202101)에서 현재월(sysdate기준)까지 매월 데이터를 조회해서 2) 월별 집계데이터를 uinon all 하여 조회된 데이터를 한꺼번에 검색 loop 등 반문복을 이용해서 쉽게 구현하고 싶은데, 잘 안되네요 방법을 알고계신 고수분들 조언 부탁드리겠습니다~~ CREATE OR REPLACE PROCEDURE GET_SHOP_DATA ( RESULT OUT SYS_REFCURSOR ) IS BEGEN OPEN RESULT FOR SELECT '202101' AS YYYYMM ,SHOP AS SHOP ,COUNT(*) AS CNT FROM MYDATA WHERE '202101' BETWEEN WORKSTART AND WORKEND GROUP BY SHOP UNION ALL SELECT '202102' AS YYYYMM ,SHOP AS SHOP ,COUNT(*) AS CNT FROM MYDATA WHERE '202102' BETWEEN WORKSTART AND WORKEND GROUP BY SHOP; UNION ALL SELECT '202103' AS YYYYMM ,SHOP AS SHOP ,COUNT(*) AS CNT FROM MYDATA WHERE '202103' BETWEEN WORKSTART AND WORKEND GROUP BY SHOP; -- 현재 월 까지 UNION ALL -- END
WITH ym AS ( SELECT TO_CHAR(ADD_MONTHS(dt, LEVEL - 1), 'yyyymm') yyyymm FROM (SELECT TO_DATE('202101', 'yyyymm') dt FROM dual) CONNECT BY LEVEL <= MONTHS_BETWEEN(TRUNC(sysdate, 'mm'), dt) + 1 ) SELECT a.yyyymm , b.shop , COUNT(*) cnt FROM ym a , mydata b WHERE a.yyyymm BETWEEN b.workstart AND b.workend GROUP BY a.yyyymm, b.shop ORDER BY a.yyyymm, b.shop ;