Hi all, I've got a script that I'm almost sure should be able to be expressed as a pivot table or a single query (but I don't know anything about them.) i'd like a few pointers because I have to deal with a lot of these types of problems. Currently the script uses a cursor :(This script gets a dataset of employee ID's and a set of benefit records (there can be many benefit records per employee). I just want to bring back the status of the most recent benefit record per employee (highest Id). Here's the script as it stands (which works but is rather ugly)DECLARE @employeeId INTDECLARE @emplist TABLE (staffNumber VARCHAR(50) COLLATE Latin1_General_CS_AS ,employeeId INT ,ebrId INT PRIMARY KEY ,ebrStatus CHAR)DECLARE @results TABLE (staffNumber VARCHAR(50) COLLATE Latin1_General_CS_AS PRIMARY KEY ,employeeId INT ,ebrId INT ,ebrStatus CHAR) SET NOCOUNT ON-- Get Search Set INSERT INTO @emplist SELECT e.staffnumber, e.id, ebr.id, ebr.status FROM employee e INNER JOIN employeeBenefitRecord ebr ON e.id = ebr.employeeId DECLARE empcur CURSOR FOR SELECT DISTINCT employeeId FROM @emplistOPEN empcur FETCH NEXT FROM empcur INTO @employeeId WHILE @@FETCH_STATUS=0 BEGIN INSERT INTO @results SELECT TOP 1 el.StaffNumber, el.employeeId, el.ebrId, el.ebrStatus FROM #emplist el WHERE el.employeeId = @employeeId ORDER BY el.ebrId DESC FETCH NEXT FROM empcur INTO @employeeId ENDCLOSE empcurDEALLOCATE empcur-- Display resultsSELECT * FROM @results ORDER BY StaffNumber
-------Anyone willing to enlighten me? All replies appreciated.-------------Charlie