Re: Help with simple query

From: Frank Bax <fbax(at)sympatico(dot)ca>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with simple query
Date: 2005-12-29 00:41:32
Message-ID: 5.2.1.1.0.20051228193054.00a3e0a0@pop6.sympatico.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 06:58 PM 12/28/05, Collin Peters wrote:
>The following query will return me all the latest dates, but I can't
>return the note_id or subject with it.
>SELECT n.user_id, max(n.modified_date)
>FROM notes n
>GROUP by n.user_id
>ORDER BY n.user_id
>
>Is this simpler than I am making it?

No, it's not "simple". You need to join the results of above sql back to
the original table (and relocate the order by clause):

SELECT notes.note_id, notes.user_id, maxx.max_date, notes.note
FROM
(SELECT n.user_id, max(n.modified_date) AS max_date FROM notes n GROUP by
n.user_id)
AS maxx
JOIN notes on notes.user_id = maxx.user_id AND notes.modified_date =
maxx.max_date
ORDER BY notes.user_id;

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-12-29 00:43:17 Re: Help with simple query
Previous Message Collin Peters 2005-12-28 23:58:12 Help with simple query