Following is a sample data of the:-
emp doa
1 2014-01-01 00:00:00.000
1 2014-01-05 00:00:00.000
1 2014-01-08 00:00:00.000
1 2014-01-12 00:00:00.000
1 2014-01-15 00:00:00.000
2 2014-01-01 00:00:00.000
2 2014-01-05 00:00:00.000
2 2014-01-10 00:00:00.000
2 2014-01-12 00:00:00.000
2 2014-01-15 00:00:00.000
3 2014-01-01 00:00:00.000
3 2014-01-05 00:00:00.000
4 2014-01-10 00:00:00.000
4 2014-01-12 00:00:00.000
4 2014-01-15 00:00:00.000
doa - Date of assigning a project
The requirement is to select the records which is closest to today - giving higher preference to past dates - which would indicate current assignment.
The expected results are-
emp doa
1 2014-01-08 00:00:00.000
2 2014-01-05 00:00:00.000
3 2014-01-05 00:00:00.000
4 2014-01-10 00:00:00.000
There are a few restrictions which I have:-
The database is in SQL Server 2008 R2. Looking ahead for a solution.
This seems like a job for row_number()
. The hard part is getting the order by
clause correct. I think the following encapsulates the logic you are looking for:
select ed.*
from (select ed.*,
row_number() over (partition by emp
order by (case when doa <= getdate() then 0 else 1 end),
abs(datediff(day, doa, getdate()))
) as seqnum
from empdoa ed
) ed
where seqnum = 1;