2017-12-04 2 views
0

두 개의 다른 쿼리가 있습니다 (결과에서 동일한 수의 열을 가짐). 둘 다 하나의 테이블에 넣고 싶습니다. 지금두 개의 서로 다른 쿼리 결과를 하나의 테이블에 추가하십시오.

id  country  salary 
1  us  10000 

2  uk  25000 

3  us  35000 

4  uk  31000 

5  uk  26000 

나는 다음과 같은 한 질의 :

쿼리 1 :

select * from table where country='us'; 

질의 2 :

예를 들어 내가 다음 표했다

내가 마지막으로 테이블과 같은 6 개 열을 가지고 있습니다 3,898,613,210

:

id1 |country1 | salary 1 | id2 | country2 | salary2 

지금, 내가 두 쿼리를 넣을 때문에 출력이 표시되어야 다음이 표에 결과 :

원하는 출력 :

id1 |country1 | salary 1 | id2 | country2 | salary2 
1 |  us | 10000 | 2 |  uk | 25000 

3 |  us | 35000 | 4 |  uk | 31000 

null | null | null  | 5 |  uk | 26000 

나는이 시도하지만 결과 결합하지 않습니다

insert into table (id1,country1,salary1) 
select id,country,salary 
from table1 
where country='us'; 

insert into table (id2,country2,salary2) 
select id,country,salary 
from table1 
where country='uk'; 

하지만 다음과 같은 결과를 제공합니다

id1 |country1 | salary 1 | id2  | country2 | salary2 
1 |  us  |  10000 |  null |  null |  null 

3  | us  | 35000 | null | null  | null 

null | null  | null  | 2  | uk  | 25000 

null | null  | null  | 4  | uk  | 31000 

null | null  | null  | 5  | uk  | 26000 

나 좀 도와주십시오

+0

하나의 삽입 문에 추가하는 대신이 삽입 문 조인 어떻게 –

+0

? 삽입 문에서 조인을 사용하는 방법을 모르겠다. – Paras

+0

사용중인 DBMS의 태그를 추가하십시오 –

답변

0

당신의 DBMS 지원 윈도우 함수 경우, 당신은 당신의 중간 결과를 적절하게 결합하는 데 사용할 수 있습니다 .

select t1.id, t1.country, t1.salary, t2.id, t2.country, t2.salary 
from 
(
    select *, row_number() over (order by id) rn 
    from data 
    where country = 'us' 
) t1 
full join 
(
    select *, row_number() over (order by id) rn 
    from data 
    where country = 'uk' 
) t2 on t1.rn = t2.rn 

demo

결과

id  country salary id country salary 
------------------------------------------- 
1  us  10000 2 uk  25000 
3  us  35000 4 uk  31000 
null null null 5 uk  26000 
당신이 사용할 수있는
+0

https://stackoverflow.com/questions/47630095/reconciliation-automation-query .....이 문제를 해결할 수 있습니까? – Paras

관련 문제