> Hmm, PostgreSQL has a non-SQL-standard extension: 'distinct on (expr)':
> I think it might do exactly what you want (n.b. I haven't been following
> this whole thread, just say this comment)
>
> test=# select distinct on (inst) inst, lastname from people
> order by inst, lastname limit 3;
I need to order by a column other than the unique one (inst), so this
approach doesn't really help me. However, I managed to accomplish the
right ordering and the usage of limit like this:
select * from people
where id in
(select distinct on (inst) id from people order by inst, age)
order by age
limit 10;
This is much slower than my original approach: "select * from people
order by age" and then filter out all duplicates afterwards. But it
should help with memory shortages on the client side.
Timo