2012-08-09 102 views
0
SELECT 
    distinct 

     HRM_Employee.EmployeeId EmployeeXId, 
     ([HRM_Employee].[FirstName] +' '+ISNULL([HRM_Employee].[MiddleName],' ')+' '+ISNULL([HRM_Employee].[LastName],' ')) AS FirstName  
    -- ,[FirstName] 
      ,[HRM_Employee].[MiddleName] 
      ,[HRM_Employee].[LastName] 
      ,[HRM_Employee].[Code]   
      ,[HRM_Employee].[UserName] 
      ,[HRM_Employee].[Password] 
      ,[HRM_Employee].[DateOfBirth] 
      ,[HRM_Employee].[OriginalBirthDate] 
      ,[HRM_Employee].[Gender] 
      ,[HRM_Employee].[BloodGroup] 
      ,[HRM_Employee].[Height] 
      ,[HRM_Employee].[MaritalStatus] 
      ,[HRM_Employee].[DateOfMarriage] 
      ,[HRM_Employee].[IdentificationMark1] 
      ,[HRM_Employee].[IdentificationMark2] 
      ,[HRM_Employee].[Religion] 
      ,(SELECT [A].[FirstName] +' '+ [A].[MiddleName] +' '+ [A].[LastName] 
       FROM [dbo].[HRM_Employee] [A] WHERE [A].EmployeeId = [HRM_Transfer].[ReportingOfficerXId] 
      ) [PersonInCharge] 
      ,[HRM_Department].[Name] [DepartmentName] 
      ,[HRM_Branch].[Name] [BranchName] 
      ,[HRM_Division].[Name] [DivisionName] 
      ,[HRM_Designation].[Name] [DesignationName] 
      ,HRM_Transfer.TransferDate 
    from HRM_Employee 
    LEFT join [dbo].[HRM_Division] 
      ON [HRM_Employee].DivisionXId = [HRM_Division].DivisionId 
      JOIN [dbo].[HRM_Designation] 
      ON [HRM_Employee].DesignationXId = [HRM_Designation].DesignationId 
      JOIN [HRM_Department] 
      ON [HRM_Employee].[DepartmentXId] = [HRM_Department].[DepartmentId] 
      JOIN [HRM_Branch] 
      ON [HRM_Employee].[BranchXId] = [HRM_Branch].[BranchId] 
      INNER JOIN HRM_Transfer 
      ON HRM_Transfer.EmployeeXId=HRM_Employee.EmployeeId 
    WHERE 
     Convert(varchar(11),HRM_Transfer.TransferDate,103) <=Convert(varchar(11), getdate(),103) 
END 

當我excecute這個我得到如下輸出選擇最新日期

EmployeeXId FirstName    TransferDate 
34    Ambarish V    2012-08-09 00:00:00.000 
54    Anil N P      2012-08-09 00:00:00.000 
55   Ann Rose Abraham    2012-08-08 00:00:00.000 
55   Ann Rose Abraham    2012-08-09 00:00:00.000 
74    Anees M S      2012-08-09 00:00:00.000 

從這個我想顯示,只有最新的轉會最新數據。這是在EmployeeId 55中,我只需要顯示Transfer date 2012-08-09 00:00:00.000的數據。我想在上面的SP中做什麼修改來獲得所需的答案?請幫我解決這個問題。

回答

0

按EmployeeXId分組,然後選擇MAX(TransferDate)。

+0

在上面的sp我想添加這行嗎? – Ramdas 2012-08-09 09:58:45

+0

在上面的Sp中,我是否添加了「Employee by EmployeeShare」組,並選擇MAX(TransferDate)「」。請幫我解決這個問題... – Ramdas 2012-08-09 10:07:43

+0

請任何人都幫我解決這個問題。請.. – Ramdas 2012-08-09 10:22:14