outer join 쿼리 질문 0 2 1,603

by 손종욱 [2009.07.02 14:13:39]


질문 : 아래 1,2번 쿼리가 동일한 결과를 출력하는데, 3번쿼리와 동일한 결과를 출력하는 쿼리를 만들려고 하는데 잘안됩니다.

1. query -------------------------------------------------------------

select a.col1 acol, a.col2, b.col1 bcol, b.col2
  from
  (
      select '1' col1, 10 col2 from dual union all
      select '2' col1, 20 from dual union all
      select '3' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 20 from dual
  ) a
full outer join 
  (
      select '0' col1, 10 col2 from dual union all
      select '1' col1, 10 from dual union all
      select '2' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 30 from dual
  ) b
 on ( a.col1 = b.col1  );

2. query ----------------------------------------------------------

select a.col1 , a.col2, b.col1 bcol, b.col2
  from
  (
      select '1' col1, 10 col2 from dual union all
      select '2' col1, 20 from dual union all
      select '3' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 20 from dual
  ) a
,
  (
      select '0' col1, 10 col2 from dual union all
      select '1' col1, 10 from dual union all
      select '2' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 30 from dual
  ) b
where a.col1 = b.col1(+)
union
select a.col1 , a.col2, b.col1 , b.col2
  from
  (
      select '1' col1, 10 col2 from dual union all
      select '2' col1, 20 from dual union all
      select '3' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 20 from dual
  ) a
,
  (
      select '0' col1, 10 col2 from dual union all
      select '1' col1, 10 from dual union all
      select '2' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 30 from dual
  ) b
where a.col1(+)  = b.col1 
;

3. query --------------------------------------------------------

select a.col1 acol, a.col2, b.col1 bcol, b.col2
  from
  (
      select '1' col1, 10 col2 from dual union all
      select '2' col1, 20 from dual union all
      select '3' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 20 from dual
  ) a
full outer join 
  (
      select '0' col1, 10 col2 from dual union all
      select '1' col1, 10 from dual union all
      select '2' col1, 20 from dual union all
      select '4' col1, 20 from dual union all
      select '5' col1, 30 from dual
  ) b
 on ( a.col1 = b.col1  and b.col2='10') ;

by 마농 [2009.07.02 14:55:46]
3번 쿼리의 결과는 예상 밖이네요. 이게 정말 맞는 결과인지 의심이 가는군요.
암튼 결과의 정당성 여부를 떠나서 똑같은 결과를 얻는 쿼리를 한번 만들어 봤습니다.
WITH a AS
(
SELECT '1' col1, 10 col2 FROM dual
UNION ALL SELECT '2', 20 FROM dual
UNION ALL SELECT '3', 20 FROM dual
UNION ALL SELECT '4', 20 FROM dual
UNION ALL SELECT '5', 20 FROM dual
)
, b AS
(
SELECT '0' col1, 10 col2 FROM dual
UNION ALL SELECT '1', 10 FROM dual
UNION ALL SELECT '2', 20 FROM dual
UNION ALL SELECT '4', 20 FROM dual
UNION ALL SELECT '5', 30 FROM dual
)
SELECT *
FROM a, b
WHERE a.col1 = b.col1(+)
AND b.col2(+) = '10'
UNION
SELECT *
FROM a, b
WHERE DECODE(b.col2,'10',a.col1(+)) = b.col1
;

by 손종욱 [2009.07.02 18:21:34]
마농님 답변감사드립니다.
DECODE(b.col2,'10',a.col1(+)) = b.col1
이부분이 어떻게 처리되어 데이터가 출력되는지 이해가 잘안되네요.
위 조건 대신 아래 쿼리를 where 조건으로 넣어서 실행해도 데이터 결과값은 동일하게 나오는데, 솔직히 이해가 안되네요.
1 = case when b.col2 = 10 and a.col1(+) = b.col1 then 1
when b.col2 <> 10 and b.col1 = null then 1
else 0
end
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입