개별 컬럼별로 전부 GROUP 을 주는건 무리가 있을거 같고 위에 DATA로 컬럼별로 GROUPING이 가능한지 궁금하네요
by 현
[2010.05.17 12:33:30]
저는 N1_1 1번이 왜 2개인지 이해가 안가는데요?
by 꼬랑지
[2010.05.17 13:02:58]
WITH T AS (
SELECT 'A' SEQ, '1' N1_1, '1' N1_2, '2' N1_3, '1' N1_4, '3' N1_5, '1' N1_6, '1' N2_1, '1' N2_2, '1' N2_3, '2' N2_4, '2' N2_5 FROM DUAL UNION ALL
SELECT 'B' SEQ, '4' N1_1, '2' N1_2, '3' N1_3, '1' N1_4, '6' N1_5, '6' N1_6, '1' N2_1, '2' N2_2, '1' N2_3, '5' N2_4, '2' N2_5 FROM DUAL UNION ALL
SELECT 'C' SEQ, '6' N1_1, '2' N1_2, '3' N1_3, '1' N1_4, '2' N1_5, '2' N1_6, '2' N2_1, '1' N2_2, '2' N2_3, '1' N2_4, '3' N2_5 FROM DUAL UNION ALL
SELECT 'D' SEQ, '3' N1_1, '2' N1_2, '1' N1_3, '2' N1_4, '1' N1_5, '5' N1_6, '1' N2_1, '2' N2_2, '2' N2_3, '5' N2_4, '5' N2_5 FROM DUAL UNION ALL
SELECT 'E' SEQ, '1' N1_1, '2' N1_2, '1' N1_3, '2' N1_4, '1' N1_5, '2' N1_6, '2' N2_1, '1' N2_2, '2' N2_3, '4' N2_4, '2' N2_5 FROM DUAL
)
, COPY AS(
SELECT LEVEL,(MOD(LEVEL-1,6)+1) LV,'N'||CEIL(LEVEL/36)||'_'||(MOD(CEIL(LEVEL/6)-1,6)+1) GUBUN FROM DUAL CONNECT BY LEVEL<=66
)
SELECT T2.GUBUN,T2.LV,NVL(T1.CNT,0) CNT
FROM
(
SELECT 'N1_1'GUBUN,N1_1 ANS,COUNT(*) CNT
FROM T
GROUP BY N1_1
UNION ALL
SELECT 'N1_2'GUBUN,N1_2 ANS,COUNT(*) CNT
FROM T
GROUP BY N1_2
UNION ALL
SELECT 'N1_3'GUBUN,N1_3,COUNT(*) CNT
FROM T
GROUP BY N1_3
UNION ALL
SELECT 'N1_4'GUBUN,N1_4,COUNT(*) CNT
FROM T
GROUP BY N1_4
UNION ALL
SELECT 'N1_5'GUBUN,N1_5,COUNT(*) CNT
FROM T
GROUP BY N1_5
UNION ALL
SELECT 'N1_6'GUBUN,N1_6,COUNT(*) CNT
FROM T
GROUP BY N1_6
UNION ALL
SELECT 'N2_1'GUBUN,N2_1,COUNT(*) CNT
FROM T
GROUP BY N2_1
UNION ALL
SELECT 'N2_2'GUBUN,N2_2,COUNT(*) CNT
FROM T
GROUP BY N2_2
UNION ALL
SELECT 'N2_3'GUBUN,N2_3,COUNT(*) CNT
FROM T
GROUP BY N2_3
UNION ALL
SELECT 'N2_4'GUBUN,N2_4,COUNT(*) CNT
FROM T
GROUP BY N2_4
UNION ALL
SELECT 'N2_5'GUBUN,N2_5,COUNT(*) CNT
FROM T
GROUP BY N2_5
) T1,COPY T2
WHERE T1.GUBUN(+)=T2.GUBUN
AND T1.ANS(+)=T2.LV
--AND T2.GUBUN='N1_1'
ORDER BY T2.GUBUN,LV
위처럼 하면 결과는 나오는데요 문항이 N1_1~N2_5까지만 있는 건지 또 문항의 답변은 6번까지만 있는건지에 따라 조금은 다르겠네요.
by 대궁이
[2010.05.17 13:03:43]
아 객관식문항이면서 A~E 의 5명이 응답을 하는겁니다.
A는 1번 , B는 4번 C는 6번 D는 3번 E는 1번을 선택한거예요
by 대궁이
[2010.05.17 13:05:45]
결국 모든 컬럼별로 GROUP BY 하는방법밖에 없는건가봐요 .. 저도 GROUP 으로 하다가 너무 부하가 커서
by 마농
[2010.05.17 13:17:21]
WITH data_t AS
(
SELECT 'A' 일련번호, 1 n1_1, 1 n1_2, 2 n1_3, 1 n1_4, 3 n1_5, 1 n1_6
, 1 n2_1, 1 n2_2, 1 n2_3, 2 n2_4, 2 n2_5 FROM dual
UNION ALL SELECT 'B', 4, 2, 3, 1, 6, 6, 1, 2, 1, 5, 2 FROM dual
UNION ALL SELECT 'C', 6, 2, 3, 1, 2, 2, 2, 1, 2, 1, 3 FROM dual
UNION ALL SELECT 'D', 3, 2, 1, 2, 1, 5, 1, 2, 2, 5, 5 FROM dual
UNION ALL SELECT 'E', 1, 2, 1, 2, 1, 2, 2, 1, 2, 4, 2 FROM dual
)
, copy_t AS
(
SELECT LEVEL lv
, DECODE(LEVEL,1,'N1_1',2,'N1_2',3,'N1_3',4,'N1_4',5,'N1_5',6,'N1_6'
, 7,'N2_1',8,'N2_2',9,'N2_3',10,'N2_4',11,'N2_5') gb
FROM dual
CONNECT BY LEVEL <= 11
)
SELECT lv, gb
, COUNT(DECODE(no,1,1)) cnt_1
, COUNT(DECODE(no,2,1)) cnt_2
, COUNT(DECODE(no,3,1)) cnt_3
, COUNT(DECODE(no,4,1)) cnt_4
, COUNT(DECODE(no,5,1)) cnt_5
, COUNT(DECODE(no,6,1)) cnt_6
FROM
(
SELECT lv, gb
, DECODE(lv,1,n1_1,2,n1_2,3,n1_3,4,n1_4,5,n1_5,6,n1_6,7,n2_1,8,n2_2,9,n2_3,10,n2_4,11,n2_5) no
FROM data_t d
, copy_t c
)
GROUP BY lv, gb
ORDER BY lv
;
by 대궁이
[2010.05.17 13:23:31]
감사합니다.마농님 꼬랑지님 참고해서 좋은 쿼리를 만들어보겠습니다 현님도 감사해요
by 현
[2010.05.17 13:51:17]
흑....저만 아무것도 이해 못하는 루져가 됐군요...
by 꼬랑지
[2010.05.18 09:28:22]
호..조회양식을 변경하니 아웃조인이 필요없지는군요. SELECT절의 DECODE문으로 아웃조인을 피한다...낭중에 요긴하게 사용할 때가 있을 것 같네요. 굿 아이디어.
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.