몇일전 질문드렸는데 제가 질문을 어렵게 했었던것 같습니다..
이번엔 코드로 질문드려 볼꼐요..
select AName , BName from 테이블A
where
(AName not in (select Sam1 from 테이블B) and
AName not in (select Sam2 from 테이블B) and
AName not in (select Sam3 from 테이블B) and
AName not in (select Sam4 from 테이블B) and
AName not in (select Sam5 from 테이블B) and
AName not in (select Sam6 from 테이블B) and
AName not in (select Sam7 from 테이블B) and
AName not in (select Orgnme from 테이블B) )
or
(BName not in (select Sam1 from 테이블B) and
BName not in (select Sam2 from 테이블B) and
BName not in (select Sam3 from 테이블B) and
BName not in (select Sam4 from 테이블B) and
BName not in (select Sam5 from 테이블B) and
BName not in (select Sam6 from 테이블B) and
BName not in (select Sam7 from 테이블B) and
BName not in (select Orgnme from 테이블B) )
위 쿼리를 성능향상 할수 있는 쿼리로 바꾸면 어떤 쿼리로 바꿔야 할까요 ??
간략히 설명하자면......
테이블A ANAME 컬럼테이터가 테이블 B의
SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7, OrgName)과 비교해서 없고
테이블A BName 컬럼테이터가 테이블 B의
SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7, OrgName)과 비교해서 는
테이블 A 의 자료를 뽑는 쿼리입니다.
Comment 8
-
처리짱
2013.12.03 19:13
-
항해자™
2013.12.03 20:25
뭐 이런거 인가요??
select *
from dbo.tTest
where colA not in (
select col1 from dbo.tTest2 union all
select col2 from dbo.tTest2 union all
select col3 from dbo.tTest2 union all
select col4 from dbo.tTest2 union all
select col5 from dbo.tTest2 union all
select col6 from dbo.tTest2 union all
select col7 from dbo.tTest2
) -
항해자™
2013.12.06 09:03
union all -> union -
건우아빠
2013.12.03 21:27
두테이블을 unpivot 로 처리 한다음 비교하는 방법도 있을듯 합니다.
-
군고구마
2013.12.04 13:08
저는 이렇게 해봤습니다.
최대한 SELECT 횟수를 줄이는 것에 중점을 두었습니다.
CREATE TABLE TABLE_A
(
ANAME VARCHAR(10)
,BNAME VARCHAR(10)
)
GO
CREATE TABLE TABLE_B
(
SAM1 VARCHAR(15)
,SAM2 VARCHAR(15)
,SAM3 VARCHAR(15)
,SAM4 VARCHAR(15)
,SAM5 VARCHAR(15)
,SAM6 VARCHAR(15)
,SAM7 VARCHAR(15)
,OrgName VARCHAR(15)
)
GO
INSERT INTO TABLE_A VALUES ('이사','집이사')
INSERT INTO TABLE_A VALUES ('이삼','익살')
INSERT INTO TABLE_A VALUES ('익틀','이틀')
INSERT INTO TABLE_B VALUES ('이트','이사','2틀','익틀','익트','일특','이틀','정식이름 이사')
INSERT INTO TABLE_B VALUES ('이트','일트','2틀','익틀','익트','일특','이틀','정식이름 이틀')
SELECT * FROM TABLE_A
SELECT * FROM TABLE_B
-- 결론적으로 SELECT 쿼리
SELECT *
FROM TABLE_A
WHERE
AName NOT IN(
SELECT ORDERS
FROM
(
SELECT SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7,OrgName
FROM TABLE_B WITH(NOLOCK)
)P
UNPIVOT
(ORDERS FOR A IN(SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7,OrgName)
)AS UNP
)
OR
BNAME NOT IN(
SELECT ORDERS
FROM
(
SELECT SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7,OrgName
FROM TABLE_B WITH(NOLOCK)
)P
UNPIVOT
(ORDERS FOR A IN(SAM1, SAM2, SAM3, SAM4, SAM5, SAM6, SAM7,OrgName)
)AS UNP
)
-
군고구마
2013.12.04 13:09
그리고 이 UNPIVOT은 사용하고자 하는 테이블의 형식이 같아야 합니다.
즉 TABLE_B의 모든 값이 VARCHAR(15)/ 사용자가 임의 지정) 으로 같아야지 UNPIVOT이 됩니다.
-
항해자™
2013.12.06 09:02
질문자의 버전이 2005 인데 가능한가요?? -
건우아빠
2013.12.06 10:12
다행이 2005부터 가능 합니다..
select *
from 테이블A as a,
테이블B as b
where AName != Sam1 AND
AName != Sam2AND
AName != Sam3AND
AName != Sam4 AND
AName != Sam5 AND
AName != Sam6 AND
AName != Sam7 AND
AName != OrgName
혹시 이거일까요?