MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

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 July 23rd, 2008, 01:14 AM
icheong icheong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 8 icheong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 25 m 59 sec
Reputation Power: 0
Most Recent entry for a particular record

Hi all,
I'm having troubles finding the best way to do this so here is the problem:

Table Users
ID UserName
1 Jim
2 Bob
3 Jane
4 Mary

Table History
ID UserID StatusID InsertDate
1 1 1 2008-01-01
2 2 1 2008-01-01
3 3 1 2008-01-01
4 4 1 2008-01-01
5 1 1 2008-01-02
6 2 1 2008-01-02
7 3 2 2008-01-02
8 4 2 2008-01-02
9 1 1 2008-01-03
10 2 1 2008-01-03

Table StatusMessage
ID Message
1 "Good"
2 "Bad"

Now what I need is a query that gets me the Status Message for the most recent History entry for each user.
So for the example above I need the results

Jane "Bad" 2008-01-02
Mary "Bad" 2008-01-02
Jim "Good" 2008-01-03
Bob "Good" 2008-01-03

does this make sense?

Reply With Quote
  #2  
Old July 23rd, 2008, 03:01 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,773 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 14 h 8 m 8 sec
Reputation Power: 37
Code:
select username,
       message,
       insertDate
  from users
  join history on users.id = history.userid
  join statusmessage on history.statusid = statusmessage.id
 where insertDate =
      (select max(insertDate)
         from history as h
        where h.userid = history.userid)

Reply With Quote
  #3  
Old July 23rd, 2008, 03:32 AM
NimirRamon NimirRamon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 21 NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 59 m 47 sec
Reputation Power: 0
Quote:
Originally Posted by icheong
Hi all,
I'm having troubles finding the best way to do this so here is the problem:

Table Users
ID UserName
1 Jim
2 Bob
3 Jane
4 Mary

Table History
ID UserID StatusID InsertDate
1 1 1 2008-01-01
2 2 1 2008-01-01
3 3 1 2008-01-01
4 4 1 2008-01-01
5 1 1 2008-01-02
6 2 1 2008-01-02
7 3 2 2008-01-02
8 4 2 2008-01-02
9 1 1 2008-01-03
10 2 1 2008-01-03

Table StatusMessage
ID Message
1 "Good"
2 "Bad"

Now what I need is a query that gets me the Status Message for the most recent History entry for each user.
So for the example above I need the results

Jane "Bad" 2008-01-02
Mary "Bad" 2008-01-02
Jim "Good" 2008-01-03
Bob "Good" 2008-01-03

does this make sense?



SELECT U.UserName, S.Message, MAX(H.InsertDate)
From User U
INNER JOIN History H ON U.ID = H.UserID
Inner JOIN StatusMessage S ON H.StatusID = S.ID
GROUP BY U.UserName,S.Message

Reply With Quote
  #4  
Old July 23rd, 2008, 07:00 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,644 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 11 h 50 m
Reputation Power: 980
Quote:
Originally Posted by NimirRamon
SELECT U.UserName, S.Message, MAX(H.InsertDate)
...
nope

that will get max date for each message for each user

if the user has 3 different messages sent on 5 days (two of them as repeats) then your query returns 3 rows for that user

this is not the same as getting the latest message for each user
__________________
r937.com | rudy.ca

Reply With Quote
  #5  
Old July 23rd, 2008, 07:04 AM
NimirRamon NimirRamon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 21 NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level)NimirRamon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 15 h 59 m 47 sec
Reputation Power: 0
Whoops.... your right, I obviously wasn't thinking properly

Reply With Quote
  #6  
Old July 23rd, 2008, 07:10 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,644 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 11 h 50 m
Reputation Power: 980
tip: don't try to improve upon any solution posted by swampboogie


Reply With Quote
  #7  
Old July 23rd, 2008, 08:33 PM
icheong icheong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 8 icheong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 25 m 59 sec
Reputation Power: 0
Much appreciated.
Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Most Recent entry for a particular record


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