具有不同列数和类型的表中数据的逻辑并集
Logical union of data from tables with diverging numbers and types of columns
考虑以下三个(或更多)tables 的逻辑联合(实际上可能不会使用 'union' 关键字):
TableA: common_key int, 'TableA' as tname', taba_col1 string, taba_col2 int
TableB: common_key int, 'TableB' as tname', tabb_col1 double, tabb_col2 double, tabb_col3 string
..
TableN: common_key int, 'TableN' as tname', tabn_col1 int, tabn_col2 date, tabn_col3 date
我们想要类似的东西:
create table union_table as select * from TableA union select * from TableB .. union select * from TableN order by common_key, tname;
然后我们可以通过执行以下操作轻松查看按 common_key 分组的所有 table 数据:
select * from union_table
获得所有这些 table 的并集的合理方法是什么?注意:我们不想在这里进行连接:我们希望所有行都是分开的。
您可以使用联合查询来执行此操作,但是您必须为 select 语句中的每个 table 指定每一列;我不知道我是否会称其为可行的结构 - 在我看来它更像是一场噩梦,至少对我来说它不清楚它打算解决什么问题...
select tname, common_key, taba_col1, taba_col2, tabb_col1, tabb_col2, tabb_col3,tabn_col1, tabn_col2, tabn_col3
from (
select 'TableA' as tname, common_key, taba_col1, taba_col2, null as tabb_col1, null as tabb_col2, null as tabb_col3,null as tabn_col1, null as tabn_col2, null as tabn_col3 from TableA
union all
select 'TableB' as tname, common_key, null, null, tabb_col1, tabb_col2, tabb_col3,null, null, null from TableB
union all
select 'TableC' as tname, common_key, null, null, null, null, null,tabn_col1, tabn_col2, tabn_col3 from TableN
) a
这会产生如下输出:
tname common_key taba_col1 taba_col2 tabb_col1 tabb_col2 tabb_col3 tabn_col1 tabn_col2 tabn_col3
TableA 1 taba_col1 1 NULL NULL NULL NULL NULL NULL
TableB 1 NULL NULL 1,5 5,4 tabb_col3 NULL NULL NULL
TableC 1 NULL NULL NULL NULL NULL 1 2015-01-01 2014-10-01
如果您有很多 table 和列,一种处理方法是使用系统目录视图中的信息以动态方式构建查询。
考虑以下三个(或更多)tables 的逻辑联合(实际上可能不会使用 'union' 关键字):
TableA: common_key int, 'TableA' as tname', taba_col1 string, taba_col2 int
TableB: common_key int, 'TableB' as tname', tabb_col1 double, tabb_col2 double, tabb_col3 string
..
TableN: common_key int, 'TableN' as tname', tabn_col1 int, tabn_col2 date, tabn_col3 date
我们想要类似的东西:
create table union_table as select * from TableA union select * from TableB .. union select * from TableN order by common_key, tname;
然后我们可以通过执行以下操作轻松查看按 common_key 分组的所有 table 数据:
select * from union_table
获得所有这些 table 的并集的合理方法是什么?注意:我们不想在这里进行连接:我们希望所有行都是分开的。
您可以使用联合查询来执行此操作,但是您必须为 select 语句中的每个 table 指定每一列;我不知道我是否会称其为可行的结构 - 在我看来它更像是一场噩梦,至少对我来说它不清楚它打算解决什么问题...
select tname, common_key, taba_col1, taba_col2, tabb_col1, tabb_col2, tabb_col3,tabn_col1, tabn_col2, tabn_col3
from (
select 'TableA' as tname, common_key, taba_col1, taba_col2, null as tabb_col1, null as tabb_col2, null as tabb_col3,null as tabn_col1, null as tabn_col2, null as tabn_col3 from TableA
union all
select 'TableB' as tname, common_key, null, null, tabb_col1, tabb_col2, tabb_col3,null, null, null from TableB
union all
select 'TableC' as tname, common_key, null, null, null, null, null,tabn_col1, tabn_col2, tabn_col3 from TableN
) a
这会产生如下输出:
tname common_key taba_col1 taba_col2 tabb_col1 tabb_col2 tabb_col3 tabn_col1 tabn_col2 tabn_col3
TableA 1 taba_col1 1 NULL NULL NULL NULL NULL NULL
TableB 1 NULL NULL 1,5 5,4 tabb_col3 NULL NULL NULL
TableC 1 NULL NULL NULL NULL NULL 1 2015-01-01 2014-10-01
如果您有很多 table 和列,一种处理方法是使用系统目录视图中的信息以动态方式构建查询。