SQL Recipes
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
  #1 (permalink)  
Old 07-08-2008, 12:00 AM
Unregistered
 
Posts: n/a
Default ANY dialect question:

How to select a record based on 1 field being min/max


Hi, say the table has these films records in table called BOXOFFICE
I want latest results picked out of this:

Code:
FILM                           Updated              Week            Result
=======================================================
Indiana Jones              5/30/2008             1                          Hit
Indiana Jones              6/7/2008               2                          Hit
Hancock                     7/4/2008               1                          Hit
Hancock                     7/11/2008              2                         Average
Titanic                      12/25/1997            1                          Hit
Titanic                        2/25/1998            10                        Superhit
Titanic                        3/25/1998            14                        Blockbuster
So want to get DISTINCT film records based on MAX (updated) ie latest results; so need to return these three from above:
Code:
Indiana Jones              6/7/2008               2                          Hit
Hancock                     7/11/2008              2                         Average
Titanic                        3/25/1998            14                        Blockbuster
SELECT film, max(updated), week, result FROM box office
GROUP BY film, week, result


I don't think is the correct query.

What might work? Its a long problem I've had in sql. How to select an entire record, based on one of its fields being the min/max for that field among all the records. Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 07-22-2008, 05:13 AM
arun arun is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
arun is on a distinguished road
Default ANY answer. Re: How to select a record based on 1 field being min/max

select film,updated,week,result from boxoffice where updated=(select max(updated) from boxoffice)
....................................... the subquery will return the maximum value of column named updated which form the where condition for the outer query ie..where updated=(maximum value of column named updated)[in this case latest date]
hope this was helpful 4 u

Last edited by arun : 07-22-2008 at 12:24 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-27-2008, 09:17 AM
Unregistered
 
Posts: n/a
Default ANY answer. Re: How to select a record based on 1 field being min/max

select col1, col2, col3, col4 from table name where (select date_field from table_name where Date_field=Max(date_field))
order by col1...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not 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



All times are GMT. The time now is 10:54 PM.


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

1 2 3 4 5 6 7 8