繁体   English   中英

使用CROSS APPLY SQL语句进行搜索

[英]Search using CROSS APPLY SQL Statement

我有以下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和Customer,其中1个Job可以具有多个JobProducts,而1个Customer可以具有多个Jobs

我认为您可以将其移至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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM