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

Posted

技术标签:

【中文标题】如何从内部连接的第二个表中按列选择前 1 个顺序 desc?【英文标题】:How select top 1 order desc by column from second table in inner join? 【发布时间】:2017-01-04 10:03:51 【问题描述】:

我有这个问题:

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”)设置条件?

我只需要第二张桌子的这个条件: 前 (1) 个首字母,其中 SampleValue 不为 NULL 顺序描述

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

谢谢。

【问题讨论】:

您使用的是 mysql 还是 MS SQL Server? (不要标记未涉及的产品。) 现在一切都好... 【参考方案1】:

改用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 ...

【讨论】:

以上是关于如何从内部连接的第二个表中按列选择前 1 个顺序 desc?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 EF / EF Core 中的第二个表上实现具有某些条件的左连接?

Hive,按数组中的项目连接 2 个表

如何在pyspark中按列合并多个数据框?

用于选择前 3 个字符的 Sql 查询

我有两个带外键的表,我想在第一个表中按下外键,第二个表出现数据

内部联接仅从第二个表中选择基于日期的一行