Re: Help with simple query

From: PFC <lists(at)peufeu(dot)com>
To: "Collin Peters" <cadiolis(at)gmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with simple query
Date: 2005-12-29 01:31:50
Message-ID: op.s2iujcjfcigqcu@apollo13
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

If you want the latest by user, you can cheat a bit and use the fact that
the id's are incrementing, thus ordering by the id
is about the same as ordering by the date field. I know it can be inexact
in some corner cases, but it's a good approximation, and
very useful in practice :

SELECT user_id, max(note_id) FROM notes GROUP by user_id;

So :

SELECT * FROM notes WHERE id IN (SELECT max(note_id) FROM notes GROUP by
user_id) ;

Can postgres use the index on these max() clauses now ?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message George Pavlov 2005-12-29 01:47:40 Re: Help with simple query
Previous Message Tom Lane 2005-12-29 00:43:17 Re: Help with simple query