2016-09-18 1 views
0

외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾을 수 있습니까? 왼손잡이 보조 테이블과 우 편 사이드 테이블과 일치하는 레코드의 레코드 수를 감안할 때.외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾으십시오.

나는 그들 사이의 관계를 서로 연결하려고합니다. 나는 샘플 데이터를 입력하는 두 개의 테이블로 시도했다. 두 사람 사이에 어떤 관계도 가져올 수 없습니다. 왼쪽 사이드 테이블에서 일치하지 않는 항목의 수를 알면 어떻게 될까요? 이 숫자를 일치하는 레코드에 추가 한 다음 왼쪽 외부 조인 출력을 가져옵니다. 오른손 쪽에서 필적 할 수없는 레코드의 수를 알고 있으면 그 레코드를 해당 레코드에 추가합니다. 그것은 우리에게 바로 외부 조인 출력을 줄 것입니다.

일치하지 않는 레코드를 알지 못해도 가능합니다. 외부 조인, 오른쪽 외부 조인 및 완전 외부 조인 리턴을 남긴 레코드 수를 찾을 수 있습니까?

CREATE table table1(
    id integer, 
    name varchar(40)   
); 

CREATE table table2(
     id integer, 
    name varchar(40)   
); 

insert into table1(id,name)values(1,'ABC'); 
insert into table1(id,name)values(2,'DEF'); 
insert into table1(id,name)values(3,'GHI'); 
insert into table1(id,name)values(4,'JKL'); 
insert into table1(id,name)values(5,'JKL'); 
insert into table1(id,name)values(6,'JKL'); 

insert into table2(id,name)values(2,'ABC'); 
insert into table2(id,name)values(2,'ABC'); 
insert into table2(id,name)values(1,'ABC'); 
insert into table2(id,name)values(1,'ABC'); 
insert into table2(id,name)values(3,'ABC'); 
insert into table2(id,name)values(3,'ABC'); 
insert into table2(id,name)values(4,'ABC'); 
insert into table2(id,name)values(4,'ABC'); 
insert into table2(id,name)values(5,'ABC'); 
insert into table2(id,name)values(5,'ABC'); 

insert into table2(id,name)values(11,'ABC'); 
insert into table2(id,name)values(12,'ABC'); 
insert into table2(id,name)values(13,'ABC'); 
insert into table2(id,name)values(14,'ABC'); 
select count(*) from table1;//6 
select count(*) from table2; //14 

select count(*) from table1 inner join table2 
on table1.id=table2.id; //10 

select count(*) from table1 left outer join table2 
on table1.id=table2.id;//11 


select count(*) from table1 right outer join table2 
on table1.id=table2.id;//14 

select count(*) from table1 full outer join table2 
on table1.id=table2.id;//15 


//Unmatched records`enter code here` 
select count(*) from table1 left outer join table2 
on table1.id=table2.id 
where table2.id is null;//1 

select count(*) from table1 right outer join table2 
on table1.id=table2.id 
where table1.id is null;//4 
+0

단일 쿼리 또는 세 가지 다른 쿼리에서 필요합니까? –

+1

나는 그 질문을 이해하지 못한다.서로 다른 조인 유형으로 얻는 레코드 수를 표시하고 일치하는 레코드가없는 레코드를 계산하는 방법도 보여줍니다. 그래서 당신은 아직도 무엇을 찾고 있습니까? –

+0

그 번호를 얻는 유일한 방법은 JOIN 쿼리를 실행하는 것입니다. FULL JOIN을 실행하여 한꺼번에 3 개를 모두 얻으십시오. – Serg

답변

0

은 타의 추종을 불허하는 기록의 수에 해당 일치하는 레코드의 수를 알고 있습니다. (아마!)

귀하의 질문에 대한 대답은 아니오입니다. 얼마나 많은 레코드가 일치하는지 (또는 불일치가 없음) 모르게 기본 테이블의 카디널리티 만 알면 다른 조인 유형의 레코드 수를 확인할 수 없습니다). 간단한 정신 운동 : 두 테이블 모두 100 개의 레코드가 있습니다. 모두가 완벽하게 일대일로 일치하면 모든 조인은 내부 조인과 동일하며 모두 100 개의 레코드가 있습니다. 일치하는 것이 전혀 없으면 내부 조인에 행이없고, 단면 일괄 조인에는 100 개의 행이 있고 완전 외부 조인에는 200 개의 행이 있습니다. 이러한 경우의 유일한 차이점은 일치하는 (또는 일치하지 않는) 레코드의 수입니다.이 정보 없이는 대답을 얻을 수 없다는 것을 전혀 알 수 없습니다. 어쨌든 "일치 얼마나 많은 레코드를"잘 정의되지 않으며, 부족 아는 사실

: 영업 이익은 후속 질문을 ADDED 후

. 두 표의 모든 레코드가 일치한다고 가정하십시오. 극단적 인 경우 일치 항목은 쌍으로 표시 될 수 있습니다. 두 표에 모두 id 열이 있으며 두 표의 값은 모두 1에서 100까지의 가능한 값입니다. 그런 다음 결과에 100 개의 행이 있습니다. 한편, "id"가 어느 테이블에서도 고유하지 않다고 가정합니다. 대신,이 값은 모두 1 개의 모든 행에 100 개의 값이 있습니다. 그런 다음 첫 번째 테이블의 모든 행은 두 번째 테이블의 모든 행과 일치하며 결과 집합에는 100 x 100 = 10,000 행이 있습니다.

다음은 "일치하는 행 수"로 잘 정의 된 개념이 아닙니다. (다른 종류의) 결과 조인을 계산하려면 조인이 무엇인지, 조인 조건의 각 튜플에 대해 각 테이블의 특정 튜플을 갖는 행 수를 알아야합니다. 그런 다음 내부 조인의 결과 집합에있는 행 수는 외부 조인의 왼쪽 또는 오른쪽 또는 양쪽 테이블에서 일치하지 않는 행에 대한 추가 행과 이러한 튜플 그룹화 된 개수의 곱 합입니다.

+0

우리가 어떻게 일치하는 레코드를 알고 있다고해도, 우리는 어떻게 co 관계를 도출 할 수 있다고 가정합니다. 예제 테이블 A를 100 개, 테이블 B를 200 개, 일치 레코드를 A와 B 사이에 150 개로 구성했습니다. 외부 조인을 종료 한 레코드 수를 알 수 있습니까? 질문. – nag

0

표 2의 ID는 고유하지 않지만 table1의 ID는 고유합니다.
그래서 table2.id가 table1.id의 외래 키라고 추측합니다. 이 경우

, 전체이 SQL의 수를 결합 할 수 있습니다 가입 일치와 타의 추종을 불허 : 당신이 알고, 레코드의 총 수를 알고 있기 때문에

select 
count(distinct case when t2.id is null then t1.id end) as total_unmatched_t1, 
count(case when t1.id is null then t2.id end) as total_unmatched_t2, 
count(distinct case when t2.id is not null then t1.id end) as total_matched_t1, 
count(case when t1.id is not null then t2.id end) as total_matched_t2 
from table1 t1 
full outer join table2 t2 on (t1.id = t2.id);