다음과 같이 ID 번호와 관련된 Part 1, Part 2, Part 3, Part 4 및 Part 5 항목 목록이 있습니다.
ID | Item
-- | ----
1 | Part 1
1 | Part 2
1 | Part 3
1 | Part 4
2 | Part 1
2 | Part 2
2 | Part 4
2 | Part 5
3 | Part 2
3 | Part 4
3 | Part 5
다음과 같이 부품 열에서 ID별로 찾을 수없는 각 부품의 결과 집합을 원합니다.
ID | Item
-- | ----
1 | Part 5
2 | Part 3
3 | Part 1
3 | Part 3
누구든지 내 솔루션 레코드 세트를 생성하기 위해 SQL 쿼리를 도울 수 있습니까?
문안 인사,
웨인
두 개의 하위 쿼리 중 id
s 및 item
s 인 경우 가능한 모든 조합을 생성 한 다음 안티 패턴을 사용하여 누락 된 레코드를 식별 할 수 있습니다.cross join
select distinct
left join
select t1.id, t2.item
from (select distinct id from mytable) t1
cross join (select distinct item from mytable) t2
left join mytable t on t.id = t1.id and t.item = t2.item
where t.id is null
order by t1.id, t2.item
다음과 같이 표현할 수도 있습니다 not exists
.
select t1.id, t2.item
from (select distinct id from mytable) t1
cross join (select distinct item from mytable) t2
where not exists (
select 1
from mytable t
where t.id = t1.id and t.item = t2.item
)
order by t1.id, t2.item
아이디 | 항목 -: | : ----- 1 | 파트 5 2 | 3 부 3 | 파트 1 3 | 파트 3
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다