Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL QUERY most recent date for a person_id returning all rows

Posted on 2004-09-16
8
Medium Priority
?
3,587 Views
Last Modified: 2008-01-09
Hi Guys
I've tried for days to figure this problem out
Any help would be great.

I want the query to retrieve the most recent for each person_id along with all information contained within the row.

EXAMPLE TABLE
auto_id  person_id   grade_id   grade_date
1          1           1         12/12/2003
2          2           1         12/06/2003
3          2           2         12/07/2003
4          1           2         12/01/2004
5          1           3         24/03/2004

THE RESULTS SET I WANT IS:-
auto_id  person_id   grade_id   grade_date
3          2           2         12/07/2003
5          1           3         24/03/2004

I HAVE TRIED QUERIES

SELECT person_id, MAX(grade_date)
FROM grade_history
GROUP BY person_id
//But that only gives me two columns i need the whole row

SELECT person_id, grade_id, grade_date
FROM grade_history
WHERE grade_date=(SELECT MAX(grade_date)
              FROM grade_history);

//But that only gives me one result set of the most recent date NOT the most recent date for a person_id

IF you could shed any light on this problem it would be most welcome.


Chad
0
Comment
Question by:Chad_MacArthur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12080234
Try this. If you're using Oracle prior to 9i, or another SQL flavor that doesn't support ANSI-standard JOIN syntax, let us know and we'll give you the older structure.

SELECT gh.*
FROM grade_history gh
INNER JOIN (
 SELECT person_id, MAX(grade_date) as grade_date
 FROM grade_history
 GROUP BY person_id
) maxdate m ON gh.grade_date = m.grade_date AND gh.person_id = m.person_id
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 500 total points
ID: 12080265
Fixing a cut/paste error:

SELECT gh.*
FROM grade_history gh
INNER JOIN (
 SELECT person_id, MAX(grade_date) as grade_date
 FROM grade_history
 GROUP BY person_id
) m ON gh.grade_date = m.grade_date AND gh.person_id = m.person_id


And the non-ANSI syntax:

SELECT gh.*
FROM grade_history gh,
( SELECT person_id, MAX(grade_date) as grade_date
 FROM grade_history
 GROUP BY person_id
) m
WHERE gh.grade_date = m.grade_date AND gh.person_id = m.person_id
0
 
LVL 2

Expert Comment

by:montasirma
ID: 12080320
Try the following
SELECT person_id, MAX(grade_id), MAX(grade_data) FROM grade_history
GROUP BY person_id DESC;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:jdlambert1
ID: 12080354
montasirma, it may vary with the version of SQL, but on most systems your query structure will return the highest grade_id and grade_date independent of each other for each person_id. Chad appears to need the most recent grade_id for each person_id, and if so, he needs to correlate the person_id and maximum grade_date with the main table.
0
 
LVL 1

Expert Comment

by:sachinrp
ID: 12141214
Hi ,

This simple query will give you desired result set

SELECT * from example
where person_id||grade_date in (select person_id||MAX(grade_date)
FROM example
GROUP BY person_id)
order by grade_date


Sachin
0
 
LVL 1

Expert Comment

by:sachinrp
ID: 12141217
replace example table with grade_history
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question