我试图从我的表格作业中返回几行,但也是从表格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”无法绑定。
任何帮助将不胜感激
答案 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)