Home All Groups Group Topic Archive Search About
Author
11 Dec 2006 3:17 PM
aaronGerlich
I have been searching for the solution to a problem, I know there must
be an easier way but I have been unable to solve this problem without
using a cursor.

Here is the example.  Using the NorthWind Sql Server Database

Lets say I want to return the entire row for the most recent Order for
each employee.  I can get the   most recent date very easily, but cant
do a join on it because it is not guaranteed to be unique.   So this
isn't useful

select employeeid, max(orderDate)
from orders
group by employeeid


Alt, I would use this to simply sort it now I have the row I want at
the top of each group.  But what to do from here?  I can only think of
using a cursor and keeping track of the prevEmployeeid and when it
changes to put that record in a table.

select *
from orders
order by employeeid, orderDate desc

There has to be a simple, elegant solution for this... anyone know it?

Aaron

AddThis Social Bookmark Button