Question : Add tables to code with Inner Select

The 1st code attached runs but the result gives multiple categories and I only want the latest/last one for each employee.  The code below it is stripped down and gives me the categories I want but now I need to add all the other tables to get other fields.  

I'll award points for code on just linking one of the tables.  The table used (in the stripped down code) in the bottom code is the last linked table (of the top code) so I don't know how to change the bottom code to include all (or any) table that goes before it.  I hope all that makes sense.  Thanks in advance.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
This code runs but the results gives multiple categories and I want just the latest one.  

SELECT   DISTINCT [vtePayrollBase].[PrbEmpName]                            AS [Name],
                  [vtePayrollBase].[PrbBirthDate]                          AS [DOB],
                  [vtePayrollBase].[PrbDateLastHire]                       AS [Last Hire Date],
                  [vtePayrollBase].[PrbEmpSSN]                             AS [SSN],
                  MAX(veEmploy.[eeDateEnd])                                AS [Term Date],
                  MAX(veEmploy.[eeCategory])                               AS [Employee Type],
                  MAX(vtCheckSumm.[CsuPayPeriod])                          AS [Pay Period],
                  vtEPayrollBase.[PrbPayDiv]
                    + vtEPayrollBase.[PrbPayDept] AS [Dept],
                  Substring(RIGHT(Rtrim([CsuEmpNumber]),10),7,4)           AS [Emp No],
                  SUM(vtCheckSummHE.[CseYTDHrs])                           AS [YTD Hours],
                  MAX(vtCheckSumm.[CsuYTDGrossPay])                        AS [YTD Gross Pay]
FROM     vtCheckSumm
         INNER JOIN vtEPayrollBase
           ON vtEPayrollBase.[prbFlxIDeb] = vtCheckSumm.[CsuFlxIDEb]
              AND vtEPayrollBase.[prbDateEnd] IS NULL
         INNER JOIN vtCheckSummHE
           ON vtCheckSumm.[CsuFlxID] = vtCheckSummHE.[CseFlxIdCsu]
         INNER JOIN vEBase
           ON vtCheckSumm.[CsuFlxIDEb] = vEBase.[EbFlxId]
         INNER JOIN vEEmploy
           ON vEBase.[EbFlxID] = vEEmploy.[EeFlxIdEb]
WHERE    ((((((((((((vtCheckSumm.[CsuDateBeg] >= '#STARTDATE#'
            AND vtCheckSumm.[CsuDateBeg] <= '#ENDDATE#')
           AND Substring(RIGHT(Rtrim(vtEPayrollBase.[PrbPayDept]),4),
                         4,1) <> 3)))))))))))
GROUP BY [vtEPayrollBase].[PrbPayDiv],[CsuEmpNumber],[vtePayrollBase].[PrbEmpName],[vtePayrollBase].[PrbBirthDate],
         [vtePayrollBase].[PrbEmpSSN],[vtePayrollBase].[PrbPayDept],[vtePayrollBase].[PrbDateLastHire]


I'd like to incorporate even one other table from above into this code...
SELECT   b.[EeFlxID]     AS [MaxID],  
         b.[EeCategory],  
         b.[EeSocNumber]  
FROM     vEEmploy b  
JOIN    (SELECT a.[EeSocNumber], MAX(EeFlxID)  MaxEeFlxID
           FROM   vEEmploy a   
           GROUP BY [EeSocNumber]) a 
ON  a.[MaxEeFlxID] = b.[EeFlxID])

Answer : Add tables to code with Inner Select

Thank you for the kind words.

The  errors  go to say that every field should be prefixed with the correct table or alias name. I added "vEEmploy." where appropriate and changed one mistaken "E." with "vEEmploy." as well.

(°v°)
1:
2:
3:
4:
5:
6:
7:
8:
...
  ( SELECT vEEmploy.EeFlxID, vEEmploy.EeSocNumber, vEEmploy.EeCategory
    FROM vEEmploy JOIN
    ( SELECT EeSocNumber, MAX(EeFlxID) As MaxEeFlxID
      FROM   vEEmploy
      GROUP BY EeSocNumber
    ) F ON vEEmploy.EeFlxID = F.MaxEeFlxID
  ) E ON vEBase.EbFlxID = E.EeFlxID
Random Solutions  
 
programming4us programming4us