I have a table with 3 columns
table: StaffDepartmentAssgnment
StaffId DepartmentId AssignedFromDate
S1 Dept1 2013-02-08
S2 Dept1 2013-02-08
S3 Dept2 2013-02-01
S1 Dept2 2013-02-01
I want to find out all the StaffIds which are currently in Dept2.How do i write a query for it?
This is a DB engine independent solution
select * from
(
select StaffId, max(AssignedFromDate) as adate
from StaffDepartmentAssgnment
group by staffid
) x
inner join StaffDepartmentAssgnment y
on y.staffid = x.staffid and adate = y.AssignedFromDate
where DepartmentId = 'dept2'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With