SQL Cross Join错误无法绑定多部分标识符

时间:2013-04-23 21:20:51

标签: sql sql-server

我试图从我的表格作业中返回几行,但也是从表格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”无法绑定。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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)