来自多个具有相同和不同列的唯一 table (SQL)

Unique table from multiple ones having same and different columns (SQL)

我有多个具有不同行和字段的数据集。

dataset1
Customer_ID   Date       Category Address         City          School
4154124       1/2/2021      A      balboa st.    Canterbury   Middleton
2145124       1/2/2012      A      somewhere     world        St. Augustine
1621573       1/2/2012      A      my_street     somewhere    St. Augustine



dataset2
Customer_ID   Date           Category Country  Zipcode   
14123         12/12/2020        B       UK      EW
416412        14/12/2020        B       ES      


dataset3
Customer_ID   Date          Category School         University 
4124123       07/12/2020       C       Middleton      Oxford

我想要一个包含所有列的最终数据集(只保留一份常用列):

Customer_ID   Date       Category Address         City          School  Country  Zipcode  University
    4154124       1/2/2021      A      balboa st.    Canterbury   Middleton
    2145124       1/2/2012      A      somewhere     world        St. Augustine
    1621573       1/2/2012      A      my_street     somewhere    St. Augustine
    14123         12/12/2020        B                                    UK      EW
    416412        14/12/2020        B                                    ES      
    4124123       07/12/2020       C                            Middleton                   Oxford

左连接是否是获得预期输出的最佳方式?我如何才能将 Customer_ID 日期和类别以及重复列(例如,学校)保留一次?

您可以使用 UNION ALL 实现此目的。

SELECT Customer_ID, Date, Category, Address, City, School, '' AS Country, '' AS ZipCode, '' AS university  FROM dataset1 
UNION ALL
SELECT Customer_ID, Date, Category, '', '', '', Country, Zipcode, ''   FROM dataset2
UNION ALL
SELECT Customer_ID, Date, Category, '', '', School, '', '', University FROM dataset3