본문 바로가기

데이타베이스(DB)/ORACLE

구분자로 입력된 값을 행으로 분류후 통계 내기

WITH    RTOTAL AS
        (
            SELECT  MAIN_SEQ, COUNT(MAIN_SEQ) AS TOT
            FROM    S_REPLY
            GROUP BY MAIN_SEQ
        )
SELECT  M.MAIN_SEQ, M.SUB_SEQ, M.IKEY, M.INAME, M.GNAME, NVL(R.CNT, 0) AS CNT, NVL(T.TOT, 0) AS TOT, ROUND((NVL(R.CNT, 0)/NVL(T.TOT, 0))*100, 1) AS RND, M.SORT
FROM    S_QUEST_MULTI M
        ,(
            SELECT  IDX, QIDX, EX_SPLIT, COUNT(EX_SPLIT) AS CNT
            FROM    (
                        SELECT  IDX, QIDX, MEMBER_ID, REGEXP_SUBSTR(R.EX, '[^\;]+', 1, L.LEV) AS EX_SPLIT
                        FROM
                        (
                            SELECT  IDX, QIDX, MEMBER_ID, EX, REGEXP_COUNT(EX, '\;') AS CNT
                            FROM    S_REPLY
                        ) R
                        ,(
                            SELECT  LEVEL AS LEV
                            FROM    DUAL
                            CONNECT BY LEVEL <= 100
                        ) L
                        WHERE   L.LEV <= R.CNT
                    )
            GROUP BY IDX, QIDX, EX_SPLIT
        ) R
        ,RTOTAL T
WHERE   M.MAIN_SEQ = R.IDX(+)
AND     M.SUB_SEQ  = R.QIDX(+)
AND     M.IKEY     = R.EX_SPLIT(+)
AND     M.MAIN_SEQ = T.MAIN_SEQ(+)
ORDER BY M.MAIN_SEQ, M.SUB_SEQ, M.SORT, M.IKEY;