Connection tables by field name? (i.e. column order cannot be guaranteed)

I would like to stack tables through UNION, but do it by the name of the field, since the tables are basically the same fields, but each table may not have all the fields, and the column order may differ. For instance.

Table 1     Table 2     Table 3     Table 4
Age         Age          Age        Height
Height      Height       Height     Weight
Weight      Weight       Weight     Age
Race        Race         Gender     Gender
---         Gender 

For example, the following code may be broken in the following example:

      SELECT * FROM TABLE 1 
UNION ALL SELECT * FROM TABLE 2
UNION ALL SELECT * FROM TABLE 3
UNION ALL SELECT * FROM TABLE 4

A motivating example is polls with quarterly or annual waves. Sometimes the fields drop out, while others are added every year. I would now like to create a process that does not require excessive attention when displaying past changes or breaks as a result of minor future changes.

, SAS, . SQL, "UNION ALL BY NAME" - ?

+3
2

BNF SQL :

<non-join query expression>    ::=
   <non-join query term>
 | <query expression body> UNION  [ALL|DISTINCT] [ <corresponding spec> ] <query term>
 | <query expression body> EXCEPT [ALL|DISTINCT] [ <corresponding spec> ] <query term> 
<corresponding spec>           ::= CORRESPONDING
                         [ BY <left paren> <corresponding column list> <right paren> ]
<corresponding column list>    ::=   <column name list> 
<column name list>    ::=   <column name> [ { <comma> <column name> }... ] 

, CORRESPONDING , . - , .

CORRESPONDING , UNION.

+4

, , , - ,

, ,

SELECT age, height, weight, gender, race FROM TABLE 1 
UNION ALL SELECT age, height, weight, gender, race FROM TABLE 2
UNION ALL SELECT age, height, weight, gender, race FROM TABLE 3
UNION ALL SELECT age, height, weight, gender, race FROM TABLE 4

, , 3 , .

SELECT age, height, weight, gender, null AS race FROM TABLE 3 

null.

+4

Source: https://habr.com/ru/post/1796436/


All Articles