I have one table vwuser. I want join this table with the table valued function fnuserrank(userID). So I need to cross apply with table valued function:
SELECT *
FROM vwuser AS a
CROSS APPLY fnuserrank(a.userid)
For each userID it generates multiple records. I only want the last record for each empid that does not have a Rank of Term(inated). How can I do this?
Data:
HistoryID empid Rank MonitorDate 1 A1 E1 2012-8-9 2 A1 E2 2012-9-12 3 A1 Term 2012-10-13 4 A2 E3 2011-10-09 5 A2 TERM 2012-11-9
From this 2nd record and 4th record must be selected.
In SQL Server 2005+ you can use this Common Table Expression (CTE) to determine the latest record by MonitorDate that doesn’t have a Rank of ‘Term’:
Note: The statement before this CTE will need to end in a semi-colon. Because of this, I have seen many people write them like
;WITH EmployeeData AS...