如何合并 SQL 服务器中具有映射到通用描述的不同列 headers 的表?
How to merge tables in SQL server that have a different column headers that map to a common description?
我想将多个 table 合并为一个 table。 table 表示相同的数据,但具有不同的列 header,因为它们来自多个来源。例如,在表 1 中,我们将有 "Tel_Nbr"、表 2 "TelNumber" 和表 3 "TelNum" 等。为了更好的可维护性,我没有重命名每个 table 中的列,而是我想我可以创建一个映射 table,我将每个 table 中的每个列 header 名称映射到最终输出 table 中的公共描述,如下所示。但是,我不确定这是最好的方法,因为我在映射 table 中使用了 table 名称,而且我无法弄清楚查询语法。
表 1
First_Name | Last_Name | Telephone_Number
-----------------------------------------
John | Smith | 3333
Michael | Taylor | 4444
表 2
F_Name | L_Name | Tel_Nbr
--------------------------------
Joe | Lopez | 5555
Rachel | Moore | 6666
Mapping_Table
Output | Table1 | Table2
----------------------------------------
FirstName | First_Name | F_Name
LastName | Last_Name | L_Name
TelephoneNbr | Telephone_Number | Tel_Nbr
输出
FirstName | LastName | TelephoneNbr
-----------------------------------------
John | Smith | 3333
Michael | Taylor | 4444
Joe | Lopez | 5555
Rachel | Moore | 6666
正如 Gordon Linoff 指出的那样,您的要求需要动态 SQL。也就是说,从 mapping_table
的内容构建一个查询字符串,然后执行它。
考虑:
declare
@q1 nvarchar(max),
@q2 nvarchar(max),
@q nvarchar(max)
;
select
@q1 = string_agg(table1 + ' as ' + output, ', ') within group(order by output),
@q2 = string_agg(table2, ', ') within group(order by output)
from mapping_table;
set @q = 'select ' + @q1 + N' from table1 union all select ' + @q2 + ' from table2'
--debug
select @q;
-- execute
EXEC sp_executesql @q;
对于您的示例数据,生成的查询是(我添加了换行符和缩进以提高可读性):
select First_Name as FirstName, Last_Name as LastName, Telephone_Number as TelephoneNbr
from table1
union all
select F_Name, L_Name, Tel_Nbr from table2
我想将多个 table 合并为一个 table。 table 表示相同的数据,但具有不同的列 header,因为它们来自多个来源。例如,在表 1 中,我们将有 "Tel_Nbr"、表 2 "TelNumber" 和表 3 "TelNum" 等。为了更好的可维护性,我没有重命名每个 table 中的列,而是我想我可以创建一个映射 table,我将每个 table 中的每个列 header 名称映射到最终输出 table 中的公共描述,如下所示。但是,我不确定这是最好的方法,因为我在映射 table 中使用了 table 名称,而且我无法弄清楚查询语法。
表 1
First_Name | Last_Name | Telephone_Number
-----------------------------------------
John | Smith | 3333
Michael | Taylor | 4444
表 2
F_Name | L_Name | Tel_Nbr
--------------------------------
Joe | Lopez | 5555
Rachel | Moore | 6666
Mapping_Table
Output | Table1 | Table2
----------------------------------------
FirstName | First_Name | F_Name
LastName | Last_Name | L_Name
TelephoneNbr | Telephone_Number | Tel_Nbr
输出
FirstName | LastName | TelephoneNbr
-----------------------------------------
John | Smith | 3333
Michael | Taylor | 4444
Joe | Lopez | 5555
Rachel | Moore | 6666
正如 Gordon Linoff 指出的那样,您的要求需要动态 SQL。也就是说,从 mapping_table
的内容构建一个查询字符串,然后执行它。
考虑:
declare
@q1 nvarchar(max),
@q2 nvarchar(max),
@q nvarchar(max)
;
select
@q1 = string_agg(table1 + ' as ' + output, ', ') within group(order by output),
@q2 = string_agg(table2, ', ') within group(order by output)
from mapping_table;
set @q = 'select ' + @q1 + N' from table1 union all select ' + @q2 + ' from table2'
--debug
select @q;
-- execute
EXEC sp_executesql @q;
对于您的示例数据,生成的查询是(我添加了换行符和缩进以提高可读性):
select First_Name as FirstName, Last_Name as LastName, Telephone_Number as TelephoneNbr
from table1
union all
select F_Name, L_Name, Tel_Nbr from table2