Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 3rd, 2003, 02:36 AM
culler culler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 10 culler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 4 sec
Reputation Power: 0
Help with SQL query

I'm trying to figure out the SQL query that will produce this:

Given 2 tables, person and promotion, I want the latest record from the promotion table (by using thedate) for each person -

Code:
Table person:

 id | name
----+-------
  1 | John
  2 | Dave
  3 | Bill


Table promotion:

 id | personid | rank           |  thedate
----+----------+----------------+------------
  1 |        2 | Private        | 2003-03-01
  2 |        1 | Private        | 2003-04-02
  3 |        1 | Corporal       | 2003-05-04
  4 |        3 | Private        | 2003-06-05
  5 |        1 | Sargeant       | 2003-07-06
  6 |        3 | Lance Corporal | 2003-08-07
  7 |        2 | Corporal       | 2003-09-21
  

Result:

 name | rank
------+-----------------
 John | Sargeant
 Dave | Corporal
 Bill | Lance Corporal

Reply With Quote
  #2  
Old November 3rd, 2003, 03:23 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
Code:
select name,rank
from person  p, promotion ap
where p.id = ap.personid
and thedate in (select max(thedate)
from promotion
where personid = ap.personid )

Reply With Quote
  #3  
Old November 4th, 2003, 02:00 AM
culler culler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 10 culler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 4 sec
Reputation Power: 0
Thanks a heap

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Help with SQL query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway