MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

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 February 7th, 2012, 11:17 PM
cartergarth cartergarth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 90 cartergarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 33 m 8 sec
Reputation Power: 2
Code Critique - How to select rows with latest date from 2 tables

I have 2 tables:
1. users - user_id(PK), firstname, middlename, lastname
2. user_shift_schedule - shift_code(PK), user_id(FK), effectivity_date

I want to get all rows with the latest effectivity_date for each user_id.

This is what I've got so far:
[CODE]
SELECT
users.user_id
,users.firstname
,users.middlename
,users.lastname
,user_shift_schedule.shift_id
,MAX(user_shift_schedule.effectivity_date)

FROM users
JOIN user_shift_schedule

ON users.user_id=user_shift_schedule.user_id

GROUP BY user_shift_schedule.user_id
[CODE]

This would give me a somewhat desired result only the shift_id is incorrect, seems like it's selecting the most recently saved shift_id and not the shift_id that is of the same row as the latest effectivity_date of each user_id.


Any suggestion is greatly appreciated.

Thank you!

Reply With Quote
  #2  
Old February 8th, 2012, 09:29 AM
SecurityDavid SecurityDavid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 135 SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level)SecurityDavid User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 6 h 14 m
Reputation Power: 52
You don't have any type of WHERE statement, so you are getting all grouped results. I would suggest that you don't want to select MAX(user_shift_schedule.effectivity_date), what you want is to select effectivity_date WHERE effectivity_date=MAX(effectivity_date)

Code:
SELECT
users.user_id,
users.firstname,
users.middlename,
users.lastname,
user_shift_schedule.shift_id,
user_shift_schedule.effectivity_date

FROM users
JOIN user_shift_schedule

ON users.user_id=user_shift_schedule.user_id

WHERE user_shift_schedule.effectivity_date=(
          SELECT MAX(effectivity_date) FROM user_shift_schedule
          )

GROUP BY user_shift_schedule.user_id

Reply With Quote
  #3  
Old February 8th, 2012, 09:44 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,621 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 19 h 23 m 23 sec
Reputation Power: 374
An uncorrelated version...

Code:
SELECT u.*
     , x.shift_code
     , x.effectivity_date
  FROM user_shift_schedule x
  JOIN
     ( SELECT user_id
            , MAX(effectivity_date) max_effectivity_date
         FROM user_shift_schedule
        GROUP
           BY user_id
     ) y
    ON y.user_id = x.user_id
   AND y.max_effectivity_date = x.effectivity_date
 RIGHT
  JOIN users u 
    ON u.user_id = x.user_id; 

Reply With Quote
  #4  
Old February 10th, 2012, 03:51 AM
cartergarth cartergarth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 90 cartergarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 33 m 8 sec
Reputation Power: 2
Thanks anyway. I already solved this in that day. Here is what I've got:

Quote:
SELECT u.user_id,u.firstname,u.middlename,u.lastname,s.shift_id,s.effectivity_date FROM users u,user_shift_schedule s,(SELECT user_id,max(effectivity_date) maxdate FROM user_shift_schedule GROUP BY user_id) m WHERE u.user_id=s.user_id AND u.user_id=m.user_id AND s.effectivity_date=m.maxdate

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Code Critique - How to select rows with latest date from 2 tables


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 - 2012, Jelsoft Enterprises Ltd.

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