我有以下SQL语句,它以我需要的格式从数据库返回结果。但是,我想使用此查询,但添加搜索JobProducts.Serial = x
的位置如何添加此类搜索?
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 APPLY
(
SELECT TOP (1) DueDate, JobProductID, JobID, ProductID, DepartmentID
FROM JobProducts AS JobProducts_1
WHERE(JobProducts_1.JobID = J.JobID And Deleted = 0)
ORDER BY DueDate
) AS derivedtbl_1
//I know the line below wont work, but how could I achieve this?
WHERE JobProducts.Serial='123456'
查询使用以下表格Jobs,JobProducts和Customers,其中1个Job可以有多个JobProducts,1个Customer可以有多个Jobs
答案 0 :(得分:0)
我认为您可以将其移至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 OUTER 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 And
Deleted = 0 and
JobProducts.Serial='123456'
ORDER BY DueDate
) AS derivedtbl_1;