2

我试图从表 Jobs 中返回几行,但也从表 JobProducts 中返回下一个截止日期(每个 JobProduct 都有截止日期)。

到目前为止,我有以下内容

SELECT  J.CustomerID,  J.JobID, J.Status, 
J.Deleted, J.JobNo, Customers.CompanyName AS [Company], 
J.DateCreated AS [Date Created], derivedtbl_1.DueDate AS [Due Date] 
FROM Jobs  J
LEFT OUTER JOIN 
Customers ON J.CustomerID = Customers.CustomerID CROSS JOIN 
(SELECT   TOP (1) DueDate, JobProductID, JobID, ProductID, DepartmentID 
FROM      JobProducts AS JobProducts_1 
WHERE    JobProducts_1.JobID = J.JobID 
ORDER BY DueDate) 
AS derivedtbl_1 

但我收到错误无法绑定多部分标识符“J.JobID”。

任何帮助将非常感激

4

2 回答 2

5

该错误似乎来自 SQL Server。我认为您对以下内容感到困惑CROSS JOIN(这是笛卡尔积)CROSS APPLY

SELECT  J.CustomerID,  
        J.JobID, 
        J.Status, 
        J.Deleted, 
        J.JobNo, 
        Customers.CompanyName AS [Company], 
        J.DateCreated AS [Date Created], 
        derivedtbl_1.DueDate AS [Due Date] 
FROM Jobs  J
LEFT JOIN Customers 
    ON J.CustomerID = Customers.CustomerID 
CROSS APPLY (SELECT TOP (1) DueDate, 
                           JobProductID, 
                           JobID, 
                           ProductID, 
                           DepartmentID 
            FROM JobProducts AS JobProducts_1 
            WHERE JobProducts_1.JobID = J.JobID 
            ORDER BY DueDate) 
AS derivedtbl_1 
于 2013-04-23T21:25:36.030 回答
0

您可以尝试将交叉连接更改为交叉应用

CROSS APPLY
(SELECT   TOP (1) DueDate, JobProductID, JobID, ProductID, DepartmentID 
FROM      JobProducts AS JobProducts_1 
WHERE    JobProducts_1.JobID = J.JobID 
ORDER BY DueDate)
于 2013-04-23T21:26:03.023 回答