我有三张桌子:
Table1 - Customer profile
customer id
customer name
customer country
customer total sales
Table2 - Contract list
contract number
customer id
customer state
contract sales
business code
Table3 - Business index
business code
business type
每个customer
都有xxx amount of contracts
,而'customer total sales'
中的每个Table1
等于该特定客户'contract sales'
中Table2
的总和。
我要列出的是所有客户名称,每个客户的总销售额,国家/地区;还列出每个客户的州和业务类型。
我的查询尝试提取customer id, customer name, customer country, customer total sales from Table1
和customer state from Table2
以及business type from Table3
。但当我左键连接Table1.customer id - > Table2.customer id,并且左连接Table2.business代码 - > Table3.business类型时,它会提取我想要的所有数据但是每个都有xxx行的重复记录顾客。
然后,我尝试通过删除一个连接并更改连接属性来找出导致它的原因。它会在'ambiguous outer join'
时弹出消息:
答案 0 :(得分:0)
在T-SQL中:
由于表格Customer_profile
和Contract_list
都应使用customer_id
列table alias
来避免错误的列名称错误。另外要删除重复记录Distinct
应添加关键字。以下查询应该有效:
select distinct cp.customer_id,
cp.customer_name,
cp.customer_country,
cp.customer_total_sales ,
cl.customer_state,
bi.business_type
from Customer_profile cp
left join Contract_list cl on cp.customer_id = cl.customer_id
left join Business_index bi on bi.business_code = cl.business_code ;