DBILITY

세로를 5줄로만 표현하기 (ROW 고정) 본문

database/sql

세로를 5줄로만 표현하기 (ROW 고정)

DBILITY 2016. 10. 7. 20:39
반응형

ROWNUM은 WINDOW FUNCTION으로 대체할 수 있습니다.

DB2 for i Series도 OLAP FUNCTION으로 ROW_NUMBER를 지원합니다.
중요한 건 MOD~

WITH T1 (EMPNO,ENAME) AS (
    SELECT '7369','SMITH' FROM DUAL UNION ALL
    SELECT '7499','ALLEN' FROM DUAL UNION ALL
    SELECT '7521','WARD' FROM DUAL UNION ALL
    SELECT '7566','JONES' FROM DUAL UNION ALL
    SELECT '7654','MARTIN' FROM DUAL UNION ALL
    SELECT '7698','BLAKE' FROM DUAL UNION ALL
    SELECT '7782','CLARK' FROM DUAL UNION ALL
    SELECT '7788','SCOTT' FROM DUAL UNION ALL
    SELECT '7839','KING' FROM DUAL UNION ALL
    SELECT '7844','TURNER' FROM DUAL UNION ALL
    SELECT '7876','ADAMS' FROM DUAL
)
SELECT
    RN,
    SUM(DECODE(CN,1,SEQ))SEQ_1,
    SUM(DECODE(CN,1,EMPNO))EMPNO_1,
    MIN(DECODE(CN,1,ENAME))ENAME_1,
    SUM(DECODE(CN,2,SEQ))SEQ_2,
    SUM(DECODE(CN,2,EMPNO))EMPNO_2,
    MIN(DECODE(CN,2,ENAME))ENAME_2,
    SUM(DECODE(CN,3,SEQ))SEQ_3,
    SUM(DECODE(CN,3,EMPNO))EMPNO_3,
    MIN(DECODE(CN,3,ENAME))ENAME_3
FROM (
    SELECT 
		 ROWNUM SEQ
		,DECODE(MOD(ROWNUM,5),0,5,MOD(ROWNUM,5))RN
		,CEIL(ROWNUM/5)CN
		,EMPNO
		,ENAME
	FROM 
		T1 
	ORDER BY EMPNO,ENAME
) X 
GROUP BY RN 
ORDER BY RN

 

반응형
Comments