WITH T AS( SELECT '001' KEY1, '2013' YYYY, '-999' FROM_VALUE, '-10' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2012' YYYY, '-10' FROM_VALUE, '20' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2011' YYYY, '20' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2012' YYYY, '-999' FROM_VALUE, '-2' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2011' YYYY, '-2' FROM_VALUE, '5' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2013' YYYY, '5' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2012' YYYY, '-999' FROM_VALUE, '10' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2011' YYYY, '10' FROM_VALUE, '30' TO_VALUE FROM DUAL ) SELECT * FROM T WHERE T.YYYY IN ( SELECT '2013' - LEVEL + 1 AS YYYY FROM DUAL CONNECT BY LEVEL <= 3 ) ORDER BY KEY1, YYYY DESC
위와 같은 테이블과 쿼리가 있습니다.
쿼리 결과로 다음과 같은 값을 얻고 싶은데 잘 안되네요 ㅠㅠ
KEY1 YYYY FROM_VALUE TO_VALUE
001 2013 -999 -10
001 2012 -10 20
001 2011 20 999
002 2013 0 0
002 2012 -999 -2
002 2011 -2 5
003 2013 5 999
003 2012 -999 10
003 2011 10 30
위 '002'번과 같이 테이블에 없는 '2013'이 함께 나올 수 있는 쿼리를 만들어야 합니다.
다시말해서 모든 KEY1 데이터에 YYYY 컬럼인 2013, 2012, 2011 가 기준이 되어 나와야 합니다.
도와주세요...ㅠㅠ
WITH T AS( SELECT '001' KEY1, '2013' YYYY, '-999' FROM_VALUE, '-10' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2012' YYYY, '-10' FROM_VALUE, '20' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2011' YYYY, '20' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2012' YYYY, '-999' FROM_VALUE, '-2' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2011' YYYY, '-2' FROM_VALUE, '5' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2013' YYYY, '5' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2012' YYYY, '-999' FROM_VALUE, '10' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2011' YYYY, '10' FROM_VALUE, '30' TO_VALUE FROM DUAL ) SELECT S.KEY1, S.YYYY, NVL(T.FROM_VALUE, 0), NVL(T.TO_VALUE, 0) FROM T, ( SELECT * FROM (SELECT LPAD(LEVEL, 3, '0') KEY1 FROM DUAL CONNECT BY LEVEL <= 3), (SELECT '2013' - LEVEL + 1 AS YYYY FROM DUAL CONNECT BY LEVEL <= 3) ) S WHERE S.YYYY = T.YYYY(+) AND S.KEY1 = T.KEY1(+) ORDER BY S.KEY1, S.YYYY DESC
WITH T AS( SELECT '001' KEY1, '2013' YYYY, '-999' FROM_VALUE, '-10' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2012' YYYY, '-10' FROM_VALUE, '20' TO_VALUE FROM DUAL UNION ALL SELECT '001' KEY1, '2011' YYYY, '20' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2012' YYYY, '-999' FROM_VALUE, '-2' TO_VALUE FROM DUAL UNION ALL SELECT '002' KEY1, '2011' YYYY, '-2' FROM_VALUE, '5' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2013' YYYY, '5' FROM_VALUE, '999' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2012' YYYY, '-999' FROM_VALUE, '10' TO_VALUE FROM DUAL UNION ALL SELECT '003' KEY1, '2011' YYYY, '10' FROM_VALUE, '30' TO_VALUE FROM DUAL ) SELECT LPAD(gno, 3, '0') AS key1, T2.yyyy, TO_NUMBER(NVL(from_value, 0)) from_value, TO_NUMBER(NVL(to_value, 0)) to_value FROM T FULL OUTER JOIN ( SELECT ROUND((LEVEL + 1) / 3) AS gno, --값(년도) 개수로 나누기 2011 + MOD(LEVEL - 1, 3) AS yyyy --값 개수로 나머지 구하기 FROM DUAL CONNECT BY LEVEL <= 9 --키 종류수 * 년도 종류수 = 3 * 3 = 9 ) T2 ON ( T.key1 = gno AND T.yyyy = T2.yyyy ) ORDER BY gno, T2.yyyy DESC
-- 뒤늦은 model SELECT * FROM T MODEL PARTITION BY (KEY1) DIMENSION BY (YYYY) MEASURES (CAST(FROM_VALUE AS VARCHAR2(100)) FROM_VALUE, CAST(TO_VALUE AS VARCHAR2(100)) TO_VALUE) RULES ( FROM_VALUE[FOR YYYY IN ('2011', '2012','2013')] = NVL(FROM_VALUE[CV()], 0) ,TO_VALUE[FOR YYYY IN ('2011', '2012','2013')] = NVL(TO_VALUE[CV()], 0) ) ORDER BY KEY1, YYYY