如何从内连接中的第二个表中按列选择前1个顺序desc?

时间:2017-01-04 10:03:52

标签: sql sql-server

我有这个问题:

SELECT     Orders_Header.OrderID, Orders_Header.Plant, Orders_Header.OrderType, Orders_Header.ShortYear, Orders_Header.OrderNo, Orders_Header.OrderDate, 
                      Orders_Header.ReceivedDate, Orders_Header.ProductionDate, Orders_Header.DVMOrderNumber, Orders_Header.Requester, Orders_Header.RequesterPhone, 
                      Orders_Header.Substitute, Orders_Header.SubstitutePhone, Orders_Header.ProjectNumber, Orders_Header.CostCenter, Orders_Header.CostResponsable, 
                      Orders_Header.BudgedDepartment, Orders_Header.BatchNumber, Orders_Header.PartName, Orders_Header.PartNumber, Orders_Header.Drawing, 
                      Orders_Header.TotalSamples, Orders_Header.CostCategory, Orders_Header.ControlPlanType, Orders_Header.ControlPlanName, Orders_Header.Status, 
                      Orders_Header.OverallProductRating, Orders_Header.Customer, Orders_Header.Supplier, Orders_Header.StoragePolicy, Orders_Header.OrderDescription, 
                      Orders_Header.KeepSamples, Orders_Header.PicturesRequired, Orders_Header.VideoRequired, Orders_Header.AttendanceRequired, Orders_Header.ClosingDate, 
                      Orders_Header.ClosingUserName, Orders_Header.TargetDate, Orders_Header.CustomerIncomingDate, Orders_Header.ShippingMode, 
                      Orders_Header.SampleInvoiceValue, Orders_Header.ShippingCost, Orders_Header.MaterialCost, Orders_Header.Invoiceable, Orders_Header.InvoiceValue, 
                      Orders_Header.InvoiceNumber, Orders_Header.InvoiceDate, Orders_Header.Urgent, Orders_Header.SampleOrderType, Orders_Header.FinalCustomerAddress, 
                      Orders_Header.ExtraNotice, Orders_Header.SampleLabeling, Orders_Header.UploadFilePath, Orders_Header.Destination, Orders_Header.RecipientName, 
                      Orders_Header.ShippingAddress, Orders_Header.PackagingType, Orders_Header.HazardousMaterial, Orders_Header.ExpressShipment, 
                      Orders_Header.ExpressCompany, Orders_Header.ExtraQty, Orders_Header.Comments, Orders_Header.AssemblySite, Orders_Header.AssemblyPlannedDate, 
                      Orders_Header.AssemblyDate, Orders_Header.ShippmentPlannedDate, Orders_Header.ShippmentDate, Orders_Header.DelayMotive, 
                      Orders_Header.LogisticsResponsable, Orders_Header.HasBOM, Orders_Header.HasSerialComponents, Orders_Header.PrototypeComponents, 
                      Orders_Header.ReadyForAssembly, Orders_Header.OrderPriority, Orders_Header.DeliveryTime, Orders_Header.RequesterApproval, Orders_Header.ApprovedDate, 
                      Orders_Header.PlannedFinishYear, Orders_Header.PlannedFinishWeek, Orders_Header.Designer, Orders_Header.RawMaterial, Orders_Header.SpecialMaterial, 
                      Orders_Header.TestArea, Orders_Header.DesignHours, Orders_Header.Temperature, Orders_Header.TestType, Orders_Header.OrderPriority AS Expr2, 
                      Orders_Samples.Initials AS Expr1
FROM         Orders_Header INNER JOIN
                      Orders_Samples ON Orders_Header.OrderNo = Orders_Samples.OrderID
WHERE     (Orders_Header.Status = 'Engineering check' OR
                      Orders_Header.Status = 'Stand by' OR
                      Orders_Header.Status = 'In work' OR
                      Orders_Header.Status = 'Completely Defined' OR
                      Orders_Header.Status = 'Assembly' OR
                      Orders_Header.Status = 'Post-Assembly' OR
                      Orders_Header.Status = 'Document review' OR
                      Orders_Header.Status = 'Delivery approval' OR
                      Orders_Header.Status = 'Ready for shipping' OR
                      Orders_Header.Status = 'Ready for use' OR
                      Orders_Header.Status = 'Laboratory' OR
                      Orders_Header.Status = 'Packaging' OR
                      Orders_Header.Status = 'Shipping') AND (Orders_Header.OrderType = 'Sample Shop Order')
ORDER BY Orders_Header.AssemblyPlannedDate, Orders_Header.Substitute

我有一个问题,这个查询生成了5000行,因为我需要形成第二行的最后一行,SampleValue列与null不同。 我如何为第二个表(“Orders_Samples”)设置条件?

我需要这个条件仅用于第二个表: Top(1)SampleValue IS NOT NULL的首字母 订购desc

如果我没有内部联接所有日期都很好,但是当我把我的内部联接显示超出我需要的时候。我只需要表Orders_Samples中与null不同的最后一行。

感谢。

1 个答案:

答案 0 :(得分:2)

改为使用CROSS APPLY

SELECT  ...
FROM    Orders_Header
        CROSS APPLY( SELECT TOP 1 ... FROM Orders_Samples 
                     WHERE Orders_Header.OrderNo = Orders_Samples.OrderID AND 
                           SampleValue IS NOT NULL 
                     ORDER BY SomeColumn DESC) Orders_Samples
WHERE   ...
ORDER BY ...