Code Critique - How to select rows with latest date from 2 tables
Discuss How to select rows with latest date from 2 tables in the MySQL Help forum on Dev Shed. How to select rows with latest date from 2 tables MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
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.
Posts: 90
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!
__________________
Providing inbound and outbound support
Posts: 135
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
Posts: 1,621
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;
Posts: 90
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