Programing

열 수가 다른 두 테이블 결합

lottogame 2020. 9. 13. 11:35
반응형

열 수가 다른 두 테이블 결합


두 개의 테이블 (테이블 A와 테이블 B)이 있습니다.

열 수가 다릅니다. 테이블 A에 더 많은 열이 있다고 가정합니다.

이 두 테이블을 통합하고 테이블 B에없는 열에 대해 null을 얻으려면 어떻게해야합니까?


다음과 같은 열이 적은 테이블에 대해 추가 열을 null로 추가하십시오.

Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2

나는 여기에 와서 위의 대답을 따랐습니다. 그러나 데이터 유형의 순서가 일치하지 않으면 오류가 발생했습니다. 다른 답변의 아래 설명이 유용합니다.

위의 결과가 표의 열 순서와 동일합니까? 오라클은 열 순서가 엄격하기 때문입니다. 아래 예제는 오류를 생성합니다.

create table test1_1790 (
col_a varchar2(30),
col_b number,
col_c date);

create table test2_1790 (
col_a varchar2(30),
col_c date,
col_b number);

select * from test1_1790
union all
select * from test2_1790;

ORA-01790 : 표현식은 해당 표현식과 동일한 데이터 유형을 가져야합니다

보시다시피 오류의 근본 원인은 *를 열 목록 지정자로 사용함으로써 암시되는 일치하지 않는 열 순서에 있습니다. 이러한 유형의 오류는 열 목록을 명시 적으로 입력하여 쉽게 피할 수 있습니다.

test1_1790 union에서 col_a, col_b, col_c를 선택하십시오. 모두 test2_1790에서 col_a, col_b, col_c를 선택하십시오. 이 오류에 대한 더 빈번한 시나리오는 SELECT 목록에서 두 개 이상의 열을 실수로 교체 (또는 이동)하는 경우입니다.

select col_a, col_b, col_c from test1_1790
union all
select col_a, col_c, col_b from test2_1790;

또는 위의 방법으로 문제가 해결되지 않으면 다음 과 같은 열에 ALIAS만드는 방법은 무엇입니까? (쿼리는 귀하의 쿼리와 동일하지 않지만 여기서 요점은 열에 별칭을 추가하는 방법입니다.)

SELECT id_table_a, 
       desc_table_a, 
       table_b.id_user as iUserID, 
       table_c.field as iField
UNION
SELECT id_table_a, 
       desc_table_a, 
       table_c.id_user as iUserID, 
       table_c.field as iField

일반적으로 집합 기반 연산자를 사용할 때 동일한 수의 열이 필요하므로 Kangkan의 대답 이 정확합니다.

SAS SQL에는 해당 시나리오를 처리하는 특정 연산자가 있습니다.

SAS (R) 9.3 SQL 프로 시저 사용 설명서

CORRESPONDING (CORR) 키워드

CORRESPONDING 키워드는 집합 연산자가 지정된 경우에만 사용됩니다. CORR은 PROC SQL이 서수 위치가 아닌 이름으로 테이블 표현식의 열과 일치하도록합니다. 이름으로 일치하지 않는 컬럼은 OUTER UNION 연산자를 제외하고 결과 테이블에서 제외됩니다.

SELECT * FROM tabA
OUTER UNION CORR
SELECT * FROM tabB;

에 대한:

+---+---+
| a | b |
+---+---+
| 1 | X |
| 2 | Y |
+---+---+

OUTER UNION CORR

+---+---+
| b | d |
+---+---+
| U | 1 |
+---+---+

<=>

+----+----+---+
| a  | b  | d |
+----+----+---+
|  1 | X  |   |
|  2 | Y  |   |
|    | U  | 1 |
+----+----+---+

U-SQL은 유사한 개념을 지원합니다.

이름에 의한 외부 조합 ON (*)

밖의

requires the BY NAME clause and the ON list. As opposed to the other set expressions, the output schema of the OUTER UNION includes both the matching columns and the non-matching columns from both sides. This creates a situation where each row coming from one of the sides has "missing columns" that are present only on the other side. For such columns, default values are supplied for the "missing cells". The default values are null for nullable types and the .Net default value for the non-nullable types (e.g., 0 for int).

BY NAME

is required when used with OUTER. The clause indicates that the union is matching up values not based on position but by name of the columns. If the BY NAME clause is not specified, the matching is done positionally.

If the ON clause includes the “*” symbol (it may be specified as the last or the only member of the list), then extra name matches beyond those in the ON clause are allowed, and the result’s columns include all matching columns in the order they are present in the left argument.

And code:

@result =    
    SELECT * FROM @left
    OUTER UNION BY NAME ON (*) 
    SELECT * FROM @right;

EDIT:

The concept of outer union is supported by KQL:

kind:

inner - The result has the subset of columns that are common to all of the input tables.

outer - The result has all the columns that occur in any of the inputs. Cells that were not defined by an input row are set to null.

Example:

let t1 = datatable(col1:long, col2:string)  
[1, "a",  
2, "b",
3, "c"];
let t2 = datatable(col3:long)
[1,3];
t1 | union kind=outer t2;

Output:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
|    1 | a    |      |
|    2 | b    |      |
|    3 | c    |      |
|      |      |    1 |
|      |      |    3 |
+------+------+------+

demo


if only 1 row, you can use join

Select t1.Col1, t1.Col2, t1.Col3, t2.Col4, t2.Col5 from Table1 t1 join Table2 t2;

참고URL : https://stackoverflow.com/questions/2309943/unioning-two-tables-with-different-number-of-columns

반응형