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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 December 7th, 2012, 02:00 PM
BishBishop BishBishop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 BishBishop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 55 sec
Reputation Power: 0
Simple query has me stumped!

Guys, it's 8pm and I'm been beating my head against a wall for a while, any chance someone can help me with the simplest thing?

I have a table which stored user ids, event dates and event descriptions, as follows:

userid eventdate eventdesctiption
1 2012-01-01 00:00:00 a
1 2012-01-02 00:00:00 b
1 2012-01-03 00:00:00 c
1 2012-01-04 00:00:00 d
1 2012-01-05 00:00:00 e
1 2012-01-06 00:00:00 f
2 2012-02-01 00:00:00 a
2 2012-02-02 00:00:00 b
2 2012-02-03 00:00:00 c
2 2012-02-04 00:00:00 d
2 2012-02-05 00:00:00 e
2 2012-02-06 00:00:00 f


What I want to select is the latest event for each user, ie:

userid eventdate eventdesctiption
1 2012-01-06 00:00:00 f
2 2012-02-06 00:00:00 f


so:

select userid, eventdesc
from testdb
where eventdate (is the last one for each user)


Any help would be really great, thanks in advance.

PS, sorry for formatting, forum wouldn't allow me to post screenies.

Reply With Quote
  #2  
Old December 7th, 2012, 08:06 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 191 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 10 h 55 sec
Reputation Power: 41
Try this:
Code:
select userid, eventdate, eventdesc
 from  ( select userid, eventdate, eventdesc,
                row_number() over( partition by userid order by eventdate desc ) as rownum
          from  testdb )
where  rownum  =  1

Reply With Quote
  #3  
Old December 8th, 2012, 04:45 AM
BishBishop BishBishop is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 2 BishBishop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 55 sec
Reputation Power: 0
Thanks so much for your reply, appreciated. Worked great once I had aliased the subquery.

Bish.

Reply With Quote
  #4  
Old December 8th, 2012, 05:21 AM
shammat shammat is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Oct 2003
Location: Germany
Posts: 2,685 shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level)shammat User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 3 Days 19 h 59 m 5 sec
Reputation Power: 284
Other possible solutions:
Code:
select t1.userid, 
       t1.eventdate, 
       t1.eventdesc
from the_table t1
where eventdate = (select max(eventdate)
                   from the_table t2
                   where t2.userid = t1.userid);

select t1.userid, 
       t1.eventdate, 
       t1.eventdesc
from the_table t1
  join (select userid, max(eventdate)
        from the_table
        group by userid) t2
    on t1.userid = t2.userid;
But I wouldn't be surprised if spacebar's solution was more efficient (as only a single scan over the table is needed).
__________________
I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
http://forums.devshed.com/misc.php?do=bbcode#code

Tips on how to ask better questions:
http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
http://wiki.postgresql.org/wiki/SlowQueryQuestions
http://catb.org/esr/faqs/smart-questions.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Simple query has me stumped!

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap