Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

i have two tables employee and employee status ..for each employee in employee table have number of entries in employee status table..while innerjoin more than one row comes for each employee.i want each employee in employee table inner join with maximum id employee statuis entry of that particular employee..

How to do that

Thanks in Advance
Posted
Updated 5-Sep-19 1:44am
v2

SQL
select employee.*,employeestatus.*
from employee with (nolock)
inner join (
            select max([status]) as [status],empid
            from employeestatus with (nolock)
            group by empid
            )
            employeestatus on employee.empid=employeestatus.empid
 
Share this answer
 
SELECT     employee.*,employeestatus.* 
FROM       employee INNER JOIN
                      employeestatus ON employee.empid = employeestatus.empid
WHERE     (employeestatus.statusid =
                          (SELECT     MAX(statusid)
                            FROM      employeestatus AS status_temp
                            WHERE      (empid = employee.empid)))

That will get you all the columns of employee status.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900