0

我正在尝试创建一个子查询以将两个表放在一起,但什么也没得到。

代码:

SELECT DISTINCT Agreements.AgrmntID, Agreements.Status, JobSites.SiteName,
  PurchaseOrderDetails.ExtCost
FROM Agreements Inner Join
  JobSites On Agreements.CustSiteID = JobSites.CustSiteID
WHERE Agreements.Status = 2 And PurchaseOrderDetails.ExtCost = (Select
    PurchaseOrderDetails.ExtCost
  FROM PurchaseOrderDetails 
  Inner Join PurchaseOrders On PurchaseOrders.POID = PurchaseOrderDetails.POID
  WHERE PurchaseOrders.Description Like ('%', JobSites.SiteName, '%') And   
  Convert(varchar(8),PurchaseOrderDetails.TimeCreated,1) = '09/10/12')
ORDER BY JobSites.SiteName

预期输出:

AgreemntID   Status  SiteName  ExtCost
----------------------------------------
1            2       TEST01    $144.00
2            2       TEST02    $400.00

我正在使用子查询,因为除了 PurchaseOrders 下的描述之外,没有任何东西可以将 ExtCost 链接到 SiteName。

希望这是足够的信息,可以帮助我。先感谢您!!

4

2 回答 2

0

如果我认为我了解您要做什么,您可以像这样使用外部应用

SELECT DISTINCT A.AgrmntID, A.Status, JobSites.SiteName,
  PurchaseOrderDetails.ExtCost
FROM Agreements A 
Join JobSites J On J.CustSiteID = A.CustSiteID
OUTER APPLY(Select P.ExtCost From 
PurchaseOrders PO
JOIN PurchaseOrderDetails POD ON POD.POID = PO.POID
Where PO.Description Like ('%', JobSites.SiteName, '%') And   
  Convert(varchar(8),PurchaseOrderDetails.TimeCreated,1) = '09/10/12') POD
WHERE A.Status = 2 AND POD.ExtCost IS NOT NULL
ORDER BY JobSites.SiteName
于 2012-10-31T22:27:26.857 回答
0

不确定这是否只是您编写问题时的拼写错误,但是在您的 SQL 中您指定了 Agreements.Status = 2 但在您的示例中您期望状态为 3 的结果?

否则,我将首先测试您的各个子查询以确保您的数据采用您期望的格式,例如尝试运行

Select
    PurchaseOrderDetails.ExtCost
  FROM PurchaseOrderDetails 
  Inner Join PurchaseOrders On PurchaseOrders.POID = PurchaseOrderDetails.POID
  WHERE PurchaseOrders.Description Like ('%', JobSites.SiteName, '%') And   
  Convert(varchar(8),PurchaseOrderDetails.TimeCreated,1) = '09/10/12'

看看你得到了什么,等等。

于 2012-10-31T22:21:08.860 回答