外连接 returns 连接列的多个副本

outer join returns multiple copy of join columns

当我在 mssql 上执行 outer join 时,我加入的列没有合并。

这是我的代码:

select top 10 *  from customer_behaviour_1P2014  full outer join
customer_behaviour_2P2014 on customer_behaviour_1P2014.customer_identifier = customer_behaviour_2P2014.customer_identifier full outer join
customer_behaviour_3P2014 on customer_behaviour_2P2014.customer_identifier = customer_behaviour_3P2014.customer_identifier

这 returns 3 列标记为 customer_identifier,而不是 1.

我做错了什么?

如果有任何不同,我将客户标识符作为每个 table 中的索引。

您正在从所有 3 个 table 中选择所有列,并且每个 table 都有一个 customer_identifier 列(从 ON 子句推导出来)。

结果中的每个 customer_identifier 列都来自不同的 table。匹配时值将相同,或者当没有行匹配时 NULL

指定一个明确的列列表而不是 * 以避免重复值。而不是 3 个单独的 customer_identifier 列,使用 COALESCE 函数来 return 第一个非 NULL 值:

SELECT <other-columns>,
COALESCE(customer_behaviour_1P2014.customer_identifier, customer_behaviour_2P2014.customer_identifier, customer_behaviour_3P2014.customer_identifier) AS customer_identifier
FROM ...