--get unique worker idsselect distinct WorkerIdfrom MyTable a--which are activewhere IsActive=1--that don't have any worker ids on a more recent time--(ignoring status as if there's a more recent active record for --this worker we want that record, not this one; if there's a more --recent inactive record we don't want this one anyway)and not exists( select top 1 1 from MyTable b where b.WorkerId = a.WorkerId and b.LocalTime > a.LocalTime)



