I want to return the most recent ten (complete) rows from a table, but ensuring they all have unique 'alternate_id's. If there are two in the top ten with the same alternate_id I don't care which I take, but I want one of them! I thought I'd use group by as follows:
select *
from track_history_items
where type='FooType'
group by alternate_id order by created_at desc limit 10;
but this seems to be causing problems (failing to return rows with alternate_ids that are definitely in the top 10). Any ideas how I should do this properly?
* SOLUTION * (I can't post an answer as I'm a new user)
here's what I've ended up doing:
SELECT field1,
...,
max(created_at),
...,
fieldN
FROM track_history_items
where type='FooType'
group by alternate_id
order by created_at desc
limit 10
This seems to do the trick. Thought I'd post it here in case it's of use to others, or there are any mistakes with it that someone might spot!