제외한 SQL 서버
-
03-07-2019 - |
문제
여기에있는 상황은 다음과 같습니다. 사람들의 정보가 포함 된 테이블이 있습니다. 그들 중 일부는 다른 시스템에서 가져 오는 반면 일부는 수동으로 수입됩니다. 내가하고 싶은 것은 모든 사람을 끌어 당기는 것입니다. 그러나 수동으로 입력 한 레코드와 수입 된 레코드가 있으면 가져온 것만 선택하고 싶습니다 (최신 정보가 있기 때문에).
여기에 내가 지금까지 가지고있는 것이 있지만 작동하지 않는 것 같습니다 (수동으로 입력 한 레코드 만 반환합니다).
SELECT --fields go here
FROM
(
SELECT PERSON_UID, instype
FROM AdmitsInterfaceTable
WHERE instype='M' --Manually entered people
EXCEPT
SELECT PERSON_UID, instype
FROM AdmitsInterfaceTable
WHERE instype='I' --Imported people
) P,
AdmitsInterfaceTable A
WHERE
P.PERSON_UID=A.PERSON_UID
AND P.instype=A.instype
내부 쿼리가 Instype 열을 끌어 당기기 때문에 이것이 작동하지 않는다는 느낌이 들지만, 더 나은 방법을 생각할 수는 없습니다. 어떤 충고?
해결책
SELECT PERSON_UID, instype
FROM AdmitsInterfaceTable a
WHERE instype='I' --get all imported people
union -- plus all manuals who are not also imported
SELECT PERSON_UID, instype
FROM AdmitsInterfaceTable a
WHERE instype='M' --Imported people
and not exists ( -- without imported matches
select * from FROM AdmitsInterfaceTable b
WHERE instype='I' and b.person_uid = a.person_uid);
다른 팁
당신은 시도 할 수 있습니다 :
SELECT CASE WHEN b.person_UID is null THEN a.person_UID ELSE person_UID END,
CASE WHEN b.person_UID is null THEN a.instype ELSE instype END
--and so forth through all the columns you want
FROM
(SELECT PERSON_UID, instype -- add list of additional fields you need here
from AdmitsInterfaceTable WHERE instype='M' ) A
LEFT JOIN
(SELECT PERSON_UID, instype -- add list of additional fields you need here
from AdmitsInterfaceTable WHERE instype='I' ) B on A.PERSON_UID = B.PERSON_UID
SELECT --fields go here
FROM AdmitsInterfaceTable A
WHERE
P.PERSON_UID=A.PERSON_UID
AND P.instype=A.instype
AND instype='M' --Manually entered people
AND person_uid not in (
SELECT PERSON_UID FROM AdmitsInterfaceTable
WHERE instype='I' --Imported people
)
제휴하지 않습니다 StackOverflow