I’m trying to build a query that will sort by Name and Year, filter duplicates with higher years and return those records in a table. My data currently looks like this:
ID-----Name-----Year
1 Bob 2010
2 John 2014
3 Bob 2004
4 Eric 2005
The data should be primarily sorted by Name then secondarily sorted by Year in a descending fashion, like so:
ID-----Name-----Year
3 Bob 2004
1 Bob 2010
4 Eric 2005
2 John 2014
Then duplicate Name records should be filter returning only the lowest Year like so:
ID-----Name-----Year
3 Bob 2004
4 Eric 2005
2 John 2014
I’ve tried things like SELECT DISTINCT and HAVING COUNT but I can’t seem to get it. Probably missing something simple. Any help?
SQL FIddle Example #1
If you can’t use
PARTITION, you can do this:SQL Fiddle Example #2