选择内部连接后的最新日期

时间:2018-03-21 17:07:41

标签: sql sql-server greatest-n-per-group

使用SQL Server,我有一个SQL视图,用于将数据集成到我们的财务软件中。该视图当前看起来像这样,每个employee_id返回一行:

SELECT        TOP (100) PERCENT dbo.EmployeeMaster.Employee_ID, dbo.EmployeeMaster.Branch_ID, dbo.EmployeeMaster.ModifyDatetime AS EmployeeMasterModified, 
                         dbo.EmployeePRMaster.ModifyDatetime AS PRMasterModified, dbo.EmployeePRLocal.ModifyDatetime AS EmployeePRLocalModified, dbo.EmployeePRState.ModifyDatetime AS EmployeePRStateModified, 
                         dbo.FMT_CLEAN_STRING(dbo.EmployeeMaster.First_Name, 'abcdefghijklmnopqrstuvwxyz') AS First_Name_Cleaned, dbo.FMT_CLEAN_STRING(dbo.EmployeeMaster.Last_Name, 'abcdefghijklmnopqrstuvwxyz') 
                         AS Last_Name_Cleaned, dbo.EmployeeMaster.Address1, dbo.EmployeeMaster.Address2, dbo.EmployeeMaster.City, dbo.EmployeeMaster.StateCode, dbo.EmployeeMaster.PostalCode, 
                         dbo.EmployeeMaster.Country, dbo.EmployeeMaster.Phone, dbo.EmployeeMaster.Phone2, dbo.EmployeePRMaster.SocialSecurityNumber, dbo.EmployeePRMaster.Gender, dbo.EmployeePRMaster.MaritalStatus, 
                         dbo.EmployeePRMaster.Birthdate, dbo.EmployeePRMaster.HireDate, dbo.EmployeePRMaster.HireDateAdjusted, dbo.EmployeePRMaster.CheckHandlingCode, dbo.EmployeePRMaster.FedExemptions, 
                         dbo.EmployeePRMaster.FedFilingStatus, dbo.EmployeePRMaster.FedAdditionalWith, dbo.EmployeePRLocal.LocalCode, dbo.EmployeePRLocal.Exemptions, dbo.EmployeePRLocal.AdditionalAmount, 
                         dbo.EmployeePRState.StateCode AS StateTaxCode, dbo.EmployeePRState.IsExemptBlind, dbo.EmployeePRState.IsExemptBlindSpouse, dbo.EmployeePRState.IsExemptOver65, 
                         dbo.EmployeePRState.IsExemptSelf, dbo.EmployeePRState.IsExemptSpecial, dbo.EmployeePRState.IsExemptSpouse, dbo.EmployeePRState.IsExemptSpouse65, dbo.EmployeePRState.Dependents, 
                         dbo.EmployeePRState.AdditionalAmount AS StateAdditional, dbo.CodePREthnicOrigin.Description, dbo.EmployeeMaster.Status, dbo.EmployeePRLocal.FilingStatus, dbo.OrderAssignment.End_Actual_Date
FROM            dbo.EmployeeMaster LEFT OUTER JOIN
                         dbo.EmployeePRLocal ON dbo.EmployeeMaster.Employee_ID = dbo.EmployeePRLocal.Employee_ID INNER JOIN
                         dbo.EmployeePRMaster ON dbo.EmployeeMaster.Employee_ID = dbo.EmployeePRMaster.Employee_ID INNER JOIN
                         dbo.EmployeePRState ON dbo.EmployeeMaster.Employee_ID = dbo.EmployeePRState.Employee_ID INNER JOIN
                         dbo.CodePREthnicOrigin ON dbo.EmployeePRMaster.EthnicOrigin = dbo.CodePREthnicOrigin.Code INNER JOIN
                         dbo.OrderAssignment ON dbo.EmployeeMaster.Employee_ID = dbo.OrderAssignment.Employee_ID
WHERE        (dbo.EmployeeMaster.ModifyDatetime > DATEADD(Day, - 14, GETDATE())) AND (dbo.EmployeeMaster.Branch_ID <> 'U') AND (dbo.EmployeeMaster.Status = '1') OR
                         (dbo.EmployeeMaster.Branch_ID <> 'U') AND (dbo.EmployeePRMaster.ModifyDatetime > DATEADD(Day, - 14, GETDATE())) OR
                         (dbo.EmployeeMaster.Branch_ID <> 'U') AND (dbo.EmployeePRLocal.ModifyDatetime > DATEADD(Day, - 14, GETDATE())) OR
                         (dbo.EmployeeMaster.Branch_ID <> 'U') AND (dbo.EmployeePRState.ModifyDatetime > DATEADD(Day, - 14, GETDATE()))

OrderAssignment.End_Actual_date是一对多的,因此会返回多行。我想只获得最近的日期(每个employee_id)。我可以使用以下查询单独完成此操作:

select TOP 1 Employee_ID,End_Actual_Date from OrderAssignment
Order by end_actual_date DESC

但是,我很难理解如何将两者基本结合起来。想法?

1 个答案:

答案 0 :(得分:1)

将内部联接修改为dbo.OrderAssignment表,使用ROW_NUMBER FUNCTION列出End_Actual_Date desc顺序中多行的顺序

$(function () {
  $('[data-toggle="tooltip"]').tooltip({trigger : 'hover'})
})

格式化道歉,我正在使用手机