SQL - 连接没有公共列的表
SQL - Joining tables with no common column
这是新手,如有任何提示,我们将不胜感激!
我有以下查询,我需要的最后一点数据是引入 CONTACT.DATUS。问题是 CONTACT table 与我已经在使用的任何 table 没有任何共同点(据我所知)。我可以通过 tables SO -> CUSTOMER -> CONTACT 来 link 他们,但我一点也不知道这是否可能。您可以在上次连接中看到我尝试执行此操作的位置,但显然这不会起作用。
感谢你们给我的帮助!
Select DISTINCT
so.num AS Ref
, so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, contact.datus AS Buyer_Email
, qbclass.name AS Class
, carrier.name AS Carrier
, CAST(soitem.datescheduledfulfillment as date) AS Fulfillment_Date
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
JOIN carrier
ON so.carrierid=carrier.id
JOIN contact
ON so.customerid=customer.id
ON customer.accountid=contact.accountid
WHERE CAST(soitem.datescheduledfulfillment as date) = '5/16/16'
AND qbclass.name<>'C- Online' AND qbclass.name<>'InterCompany'
现在,您似乎要撤回客户的所有联系人。我假设客户是一家公司(例如 Acme Inc.),联系人是 Acme Inc 的员工(Bugs Bunny、Daffy Duck 等)。除非你在联系人 table 上有某种标志表明你想要返回哪个联系人,否则你当前的联系人加入将拉动所有人并且可能为每个联系人创建一个重复行。
您的 JOIN 逻辑非常接近,但您似乎跳过了一个步骤:
在加入 Contact 之前,您需要加入 Customer。现在,它只是被扔进了您的 JOIN 子句;明确地做。
Select DISTINCT
so.num AS Ref
, so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, contact.datus AS Buyer_Email
, qbclass.name AS Class
, carrier.name AS Carrier
, CAST(soitem.datescheduledfulfillment as date) AS Fulfillment_Date
, contacts.DATUS AS DATUS
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
JOIN carrier
ON so.carrierid=carrier.id
--My Change--
JOIN customer
ON so.customerid = customer.id
JOIN contact
--Removed this ON so.customerid=customer.id--
ON customer.accountid=contact.accountid
--Done My Change--
WHERE CAST(soitem.datescheduledfulfillment as date) = '5/16/16'
AND qbclass.name<>'C- Online' AND qbclass.name<>'InterCompany'
您的加入并不都需要基于您的起始 table,所以。在这种情况下,从 SO 开始,然后在 SO.customerid = Customers.ID 加入客户,然后在 Customers.accountid = Contacts.accountid 加入联系人。再次注意,这目前会撤回与您的客户共享 AccountID 的任何联系人。
这是新手,如有任何提示,我们将不胜感激!
我有以下查询,我需要的最后一点数据是引入 CONTACT.DATUS。问题是 CONTACT table 与我已经在使用的任何 table 没有任何共同点(据我所知)。我可以通过 tables SO -> CUSTOMER -> CONTACT 来 link 他们,但我一点也不知道这是否可能。您可以在上次连接中看到我尝试执行此操作的位置,但显然这不会起作用。
感谢你们给我的帮助!
Select DISTINCT
so.num AS Ref
, so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, contact.datus AS Buyer_Email
, qbclass.name AS Class
, carrier.name AS Carrier
, CAST(soitem.datescheduledfulfillment as date) AS Fulfillment_Date
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
JOIN carrier
ON so.carrierid=carrier.id
JOIN contact
ON so.customerid=customer.id
ON customer.accountid=contact.accountid
WHERE CAST(soitem.datescheduledfulfillment as date) = '5/16/16'
AND qbclass.name<>'C- Online' AND qbclass.name<>'InterCompany'
现在,您似乎要撤回客户的所有联系人。我假设客户是一家公司(例如 Acme Inc.),联系人是 Acme Inc 的员工(Bugs Bunny、Daffy Duck 等)。除非你在联系人 table 上有某种标志表明你想要返回哪个联系人,否则你当前的联系人加入将拉动所有人并且可能为每个联系人创建一个重复行。
您的 JOIN 逻辑非常接近,但您似乎跳过了一个步骤: 在加入 Contact 之前,您需要加入 Customer。现在,它只是被扔进了您的 JOIN 子句;明确地做。
Select DISTINCT
so.num AS Ref
, so.shiptoname AS Recipient_Full_Name
, so.shiptoaddress AS Address_1
, so.shiptocity AS City
, stateconst.name AS State
, so.shiptozip AS Zip
, so.billtoname AS Buyer_Name
, contact.datus AS Buyer_Email
, qbclass.name AS Class
, carrier.name AS Carrier
, CAST(soitem.datescheduledfulfillment as date) AS Fulfillment_Date
, contacts.DATUS AS DATUS
From SO
JOIN stateconst
ON so.shiptostateid=stateconst.id
JOIN qbclass
ON so.qbclassid=qbclass.id
JOIN soitem
ON so.id=soitem.soid
JOIN carrier
ON so.carrierid=carrier.id
--My Change--
JOIN customer
ON so.customerid = customer.id
JOIN contact
--Removed this ON so.customerid=customer.id--
ON customer.accountid=contact.accountid
--Done My Change--
WHERE CAST(soitem.datescheduledfulfillment as date) = '5/16/16'
AND qbclass.name<>'C- Online' AND qbclass.name<>'InterCompany'
您的加入并不都需要基于您的起始 table,所以。在这种情况下,从 SO 开始,然后在 SO.customerid = Customers.ID 加入客户,然后在 Customers.accountid = Contacts.accountid 加入联系人。再次注意,这目前会撤回与您的客户共享 AccountID 的任何联系人。