Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
CSS
i have some  tables

name employeeshifts
Shifts 1= A,2=B,3=C

columns are
sno     Shifts      EmployeeID      Startdate                   EndDate                     ChangedDate
1       1               1           2012-12-12 00:00:00.000 2012-12-15 00:00:00.000         2012-12-13 00:00:00.000

2       2               1           2012-12-13 00:00:00.000 2012-12-18 00:00:00.000         null

3       3               1           2012-12-19 00:00:00.000 2012-12-21 00:00:00.000         null

4       1               2           2012-12-12 00:00:00.000 2012-12-15 00:00:00.000         null

5       2               4           2012-12-12 00:00:00.000 2012-12-15 00:00:00.000         null

6       3               5           2012-12-12 00:00:00.000 2012-12-15 00:00:00.000         null

7       1               9           2012-12-16 00:00:00.000 2012-12-18 00:00:00.000         2012-12-16 00:00:00.000

8       2               9           2012-07-07 00:00:00.000 2012-08-17 00:00:00.000         null

there is employee table as well

what i need to show
last record of employee where employee has performed or is performing duty

suppose
employee 1 has performed his last duty a/c to above table
2012-12-21 00:00:00.000 in C shifts
Employee 9 will perform  will be performing duty
2012-08-17 00:00:00.000 in B shifts

Just like this i need to show last record where employee performs duty

thanks
Posted

what about using top1 with
SQL
order by EndDate  desc

and add group by employeeid if needed

Regards
Sebatian
 
Share this answer
 
v2
hi,

u need the query employeeid enddate or grouping based on shifts,

If u need employeeid enddate

select * from employeeshifts group by employeeid order by endate desc

if it is on shifts with employee

select * from employeeshifts group by shifts,employeeid order by endate desc
 
Share this answer
 
If you want to get the latest record of all employees where employee has performed or is performing duty you can use the below query.

SQL
SELECT sno
, EmployeeID
, CASE WHEN Shifts = 1 THEN 'A'
       WHEN Shifts = 2 THEN 'B'
       WHEN Shifts = 3 THEN 'C' END AS Shifts
, StartDate
, EndDate
, ChangedDate
 FROM employeeshifts
WHERE sno IN
(
    SELECT MAX(sno) AS sno FROM employeeshifts
    GROUP BY EmployeeID
)
 
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