with tmp1 as (
select '1' s_id, 'kang' s_nm from dual union all
select '2' s_id, 'kimkukjin' s_nm from dual union all
select '3' s_id, 'kimkildong' s_nm from dual union all
select '4' s_id, 'seojinsu' s_nm from dual),
tmp2 as (
select '1' c_id, 'DataBase' c_nm from dual union all
select '2' c_id, 'Java' c_nm from dual),
tmp3 as (
select '1' s_id, '1' c_id , 1 chasu from dual union all
select '1' s_id, '1' c_id , 3 chasu from dual union all
select '1' s_id, '2' c_id , 2 chasu from dual union all
select '2' s_id, '1' c_id , 1 chasu from dual union all
select '2' s_id, '1' c_id , 2 chasu from dual union all
select '2' s_id, '1' c_id , 3 chasu from dual union all
select '3' s_id, '2' c_id , 1 chasu from dual union all
select '3' s_id, '2' c_id , 2 chasu from dual union all
select '4' s_id, '1' c_id , 1 chasu from dual) 아이디 성명 스터디 1차 2차 3차 참여횟수 1 강호동 Database O O 2 Java O 1 2 유재석 Database O O O 3 Java 0 3 김제동 Database 0 Java O O 2 4 김국진 Database O 1 Java 0
결과집합
좋은 하루 보내세요. ^^
정답은 여기에.. ^^ http://www.gurubee.net/lecture/2190
with tmp1 as (
select '1' s_id, 'kang' s_nm union all
select '2' s_id, 'kimkukjin' s_nm union all
select '3' s_id, 'kimkildong' s_nm union all
select '4' s_id, 'seojinsu' s_nm ),
tmp2 as (
select '1' c_id, 'DataBase' c_nm union all
select '2' c_id, 'Java' c_nm ),
tmp3 as (
select '1' s_id, '1' c_id , 1 chasu union all
select '1' s_id, '1' c_id , 3 chasu union all
select '1' s_id, '2' c_id , 2 chasu union all
select '2' s_id, '1' c_id , 1 chasu union all
select '2' s_id, '1' c_id , 2 chasu union all
select '2' s_id, '1' c_id , 3 chasu union all
select '3' s_id, '2' c_id , 1 chasu union all
select '3' s_id, '2' c_id , 2 chasu union all
select '4' s_id, '1' c_id , 1 chasu )
select
(Case When ROW_NUMBER() over (PARTITION by a.s_id Order By a.s_id,b.c_nm) = 1
Then a.s_id
Else ''
End
) As s_id
,max(a.s_nm)
,b.c_nm
,IsNull(Min(Case When c.chasu = 1 Then 'O' End),'')
,IsNull(Min(Case When c.chasu = 2 Then 'O' End),'')
,IsNull(Min(Case When c.chasu = 3 Then 'O' End),'')
, IsNull(Min(Case When c.chasu = 1 Then 1 End),0)
+ IsNull(Min(Case When c.chasu = 2 Then 1 End),0)
+ IsNull(Min(Case When c.chasu = 3 Then 1 End),0)
from tmp1 a
Full Outer Join tmp2 b On 1 = 1
Left Outer Join tmp3 c
On a.s_id = c.s_id
And b.c_id = c.c_id
group by a.s_id
,b.c_nm
order by a.s_id
,b.c_nm
with tmp1 as (
select '1' s_id, 'kang' s_nm from dual union all
select '2' s_id, 'kimkukjin' s_nm from dual union all
select '3' s_id, 'kimkildong' s_nm from dual union all
select '4' s_id, 'seojinsu' s_nm from dual),
tmp2 as (
select '1' c_id, 'DataBase' c_nm from dual union all
select '2' c_id, 'Java' c_nm from dual),
tmp3 as (
select '1' s_id, '1' c_id , 1 chasu from dual union all
select '1' s_id, '1' c_id , 3 chasu from dual union all
select '1' s_id, '2' c_id , 2 chasu from dual union all
select '2' s_id, '1' c_id , 1 chasu from dual union all
select '2' s_id, '1' c_id , 2 chasu from dual union all
select '2' s_id, '1' c_id , 3 chasu from dual union all
select '3' s_id, '2' c_id , 1 chasu from dual union all
select '3' s_id, '2' c_id , 2 chasu from dual union all
select '4' s_id, '1' c_id , 1 chasu from dual),
FINAL AS
(
SELECT *
FROM tmp1
, tmp2
WHERE 1=1
ORDER BY 1, 2
)
SELECT NULLIF(FINAL.S_ID, LAG(FINAL.S_ID) over(order by FINAL.S_ID,FINAL.C_NM)) AS S_ID
, NULLIF(MAX(S_NM), LAG(MAX(S_NM)) over(order by FINAL.S_ID,FINAL.C_NM)) AS S_NM
, C_NM
, LISTAGG(CASE WHEN CHASU = 1 THEN 'O' ELSE NULL END) WITHIN GROUP(ORDER BY 1) AS "1차"
, LISTAGG(CASE WHEN CHASU = 2 THEN 'O' ELSE NULL END) WITHIN GROUP(ORDER BY 1) AS "2차"
, LISTAGG(CASE WHEN CHASU = 3 THEN 'O' ELSE NULL END) WITHIN GROUP(ORDER BY 1) AS "3차"
, COUNT(TMP3.S_ID) AS "참여횟수"
-- ,
FROM FINAL
, TMP3
WHERE 1=1
AND TMP3.S_ID (+)= FINAL.S_ID
AND TMP3.C_ID (+)= FINAL.C_ID
GROUP BY FINAL.S_ID, C_NM
;
이 쿼리의 문제점을 혹시 아시나요 ?/////////////////////////////// ㅠㅠㅠ
재밌습니다 ㅎㅎ
WITH TMP1 AS ( SELECT '1' S_ID, 'kang' S_NM FROM DUAL UNION ALL SELECT '2' S_ID, 'kimkukjin' S_NM FROM DUAL UNION ALL SELECT '3' S_ID, 'kimkildong' S_NM FROM DUAL UNION ALL SELECT '4' S_ID, 'seojinsu' S_NM FROM DUAL) , TMP2 AS ( SELECT '1' C_ID, 'DataBase' C_NM FROM DUAL UNION ALL SELECT '2' C_ID, 'Java' C_NM FROM DUAL) , TMP3 AS ( SELECT '1' S_ID, '1' C_ID , 1 CHASU FROM DUAL UNION ALL SELECT '1' S_ID, '1' C_ID , 3 CHASU FROM DUAL UNION ALL SELECT '1' S_ID, '2' C_ID , 2 CHASU FROM DUAL UNION ALL SELECT '2' S_ID, '1' C_ID , 1 CHASU FROM DUAL UNION ALL SELECT '2' S_ID, '1' C_ID , 2 CHASU FROM DUAL UNION ALL SELECT '2' S_ID, '1' C_ID , 3 CHASU FROM DUAL UNION ALL SELECT '3' S_ID, '2' C_ID , 1 CHASU FROM DUAL UNION ALL SELECT '3' S_ID, '2' C_ID , 2 CHASU FROM DUAL UNION ALL SELECT '4' S_ID, '1' C_ID , 1 CHASU FROM DUAL) SELECT DECODE(T.C_ID, 1, T.S_ID) 아이디, DECODE(T.C_ID, 1, DECODE(T.S_NM, 'kang', '강호동', 'kimkukjin', '유재석', 'kimkildong', '김제동', 'seojinsu', '김국진')) 성명, --T.C_ID, T.C_NM 스터디, MIN(DECODE(TMP3.CHASU, 1, '○', NULL)) AS "1차", MIN(DECODE(TMP3.CHASU, 2, '○', NULL)) AS "2차", MIN(DECODE(TMP3.CHASU, 3, '○', NULL)) AS "3차", COUNT(TMP3.CHASU) AS 참여횟수 FROM (SELECT * FROM TMP1, TMP2 ) T, TMP3 WHERE T.S_ID = TMP3.S_ID(+) AND T.C_ID = TMP3.C_ID(+) GROUP BY T.S_ID, T.S_NM, T.C_ID, T.C_NM ORDER BY T.S_ID, T.C_ID