All,
I\'m sure this is a pretty simple SQL query question, but I\'m sure there\'s a good way, and a very BAD way, to do this. Left to my own devices, I\'m liable to end
@Funka, that will not work if you have duplicate "value" values for different ID's - that will basically give you a grouped list by "value", not by id...
@Joe Fair, aggregates aren't allowed in where clauses without a subquery/having combo as well, at least not in ANSI...
This will give you the list, but will give duplicates as well if you have multiple rows with the same id/As_of values:
select t1.id, t1.value, t1.As_of
from tableName t1
join (
select id as id, max(As_of) as max_as_of
from tableName
group by id
) t2
on t1.id = t2.id
and t1.As_of = t2.max_as_of
If you want to remove duplicates from that, you'd just want to add a distinct to the top select, like this:
select distinct t1.id, t1.value, t1.As_of
from tableName t1
join (
select id as id, max(As_of) as max_as_of
from tableName
group by id
) t2
on t1.id = t2.id
and t1.As_of = t2.max_as_of