SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 04-28-2006, 02:48 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default ANY dialect question:

Find the maximum value of a column and return the corresponding id?


This question seems easy at first but it isn't. I was reminded of it again on the Postgres mailing list.
(http://article.gmane.org/gmane.comp.db.postgresql.sql/16282)

I have a simple table called notes which contains notes for users.
The table has 4 columns:
note_id (auto-incrementing primary key),
user_id (foreign key to a users table),
note (varchar), and
modified_date (timestamp).

Is there a nice simple query I can run that will return me a list of
all the *latest* notes for all users (users can have many notes in the
table)? I'm trying to figure out a simple way of doing it but seem to
be having some mental block or there is no easy way to do it.

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 04-28-2006, 04:20 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
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):

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; 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-28-2006, 04:27 AM
ben ben is offline
Administrator
 
Join Date: Mar 2007
Posts: 77
ben has disabled reputation
Default SQL 99 answer. Re: Find the maximum value of a column and return the corresponding id?

Answer by: PFC
(http://article.gmane.org/gmane.comp.db.postgresql.sql/16285)

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) ;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-21-2006, 08:30 PM
nsas
 
Posts: n/a
Default MS SQL answer. Re: Find the maximum value of a column and return the corresponding id?

U need antivirus?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find the minimum or maximum value across several columns? ben SQL - Questions and Answers 5 12-04-2007 07:01 PM


All times are GMT. The time now is 09:24 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7