If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Simple query?!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-28-05, 11:37
G_Andy G_Andy is offline
Registered User
 
Join Date: Apr 2004
Location: Toronto
Posts: 3
Question Simple query?!

Hello,

My gues is there should be a simple query to do this, but...
How would you select the name of EACH user and the MOST RECENT year only for each and every one?
The table would look like this:

user year
---- ----
Andy 1984
John 1982
Lisa 2000
John 2003
Andy 2005
John 2005

And the query should yield something like:

user year
---- ----
Lisa 2000
Andy 2005
John 2005

Thank you,
Andy G.
Reply With Quote
  #2 (permalink)  
Old 09-28-05, 13:27
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool


It is a simple query, and I'm sure you can figure it out.
What have you got coded?

HINT: Use the MAX() function.
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 10-01-05, 15:36
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by G_Andy
How would you select the name of EACH user and the MOST RECENT year only for each and every one?
This is a typical situation for using a GROUP BY, since you want "summary information" from a complete table: the number of rows to show is less than the number of rows that contain the information.
E.g., the row "Andy 1984" cannot be dropped by using a WHERE condition, since its presence or not in the result table depends on other rows, in this case the row "Andy 2005".

Since you want just one row per user, grouping by user is the way to go.
This will show you ( in the result table) a single row per group.
Final thing to decide: what do you want to see per group?
Well, clearly the "user" column, which is guaranteed to be constant in each group, and then, for the "year" column, the "summary information", which is MAX(year) in your case.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 10-01-05 at 15:39.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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