I have this table:
Department NAME EMAIL ID DATE1 DATE2
1 John asd@asd.com 74 05/04/2007 05/04/2007
1 Sam asd@asd.com 23 05/04/2007 05/04/2007
1 Tom asd@asd.com 46 05/04/2007 03/04/2007
1 Bob bob@asd.com 23 01/01/2006
2 Tom asd@asd.com 62 02/02/2000 05/05/1997
I want to get the row (only one per department) with max DATE1, but it’s not unique! So if there is multiple results I want to get the max DATE2, and if there are multiple ones then the one with the biggest ID is returned.
So there result of the query would be:
1 John asd@asd.com 74 05/04/2007 05/04/2007
2 Tom asd@asd.com 62 02/02/2000 05/05/1997
Thank you very much.
You need to use the ROW_NUMBER function: