View Single Post
  #2 (permalink)  
Old 04-28-2006, 04:20 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 87
ben has disabled reputation
Default SQL 99 answer. Re: Find the maximum value of a column and return the corresponding id?

Answer by: Frank Bax
(http://article.gmane.org/gmane.comp.db.postgresql.sql/16284)

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):

[sql]
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;
[/sql]
Reply With Quote