472,982 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

How to pull the newest record for each student out of a table

Can I do this in one SQL statement? If so, please tell me how. I
have wasted a ton of time on this but there has to be an easy way to
do it. I should have done it with more than one statement and been
done with it but I can't stop thinkin about it.

Table:
Grades

Columns:
StudentID
GradeDate
Grade

I want to get the last grade for each student. However, the maximum
date for each student is different. I tried

SELECT max(GradeDate), StudentID, Grade
FROM Grades;

Didn't give me what I wanted.

Please tell me how to get the newest grade for each student.

Bill
Dec 22 '05 #1
5 3578
Is this a homework assignment? Did you forget to read the section on
summary queries?

Dec 22 '05 #2
On 21 Dec 2005 22:17:21 -0800, pi********@hotmail.com wrote:
Is this a homework assignment? Did you forget to read the section on
summary queries?


Nope. I've just forgotten how to do it.

I have the answers I need but I feel like I did it the long way. But
what is the simplest way? Can I do it in one statement?

It's not exactly what I would cosider a summary. I just want the
latest record for each ID in the table.
Dec 22 '05 #3

"Bill" <Bi*****@yahoo.com> wrote in message
news:2m********************************@4ax.com...
Can I do this in one SQL statement? If so, please tell me how. I
have wasted a ton of time on this but there has to be an easy way to
do it. I should have done it with more than one statement and been
done with it but I can't stop thinkin about it.

Table:
Grades

Columns:
StudentID
GradeDate
Grade

I want to get the last grade for each student. However, the maximum
date for each student is different. I tried

SELECT max(GradeDate), StudentID, Grade
FROM Grades;

Didn't give me what I wanted.

Please tell me how to get the newest grade for each student.

Bill


Bill, like most all of this stuff, there's more than one way to do it.
There's probably better ways, as well, but this is what I came up with:

SELECT StudentID, GradeDate, Grade
FROM Grades INNER JOIN
(SELECT Max(GradeDate) AS MaxDate, StudentID
FROM Grades GROUP BY StudentID) as g
ON (Grades.StudentID=g.StudentID) AND (Grades.GradeDate = g.MaxDate);

HTH

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.

Dec 23 '05 #4
On Fri, 23 Dec 2005 00:41:56 GMT, "Randy Harris" <ra***@SpamFree.com>
wrote:

"Bill" <Bi*****@yahoo.com> wrote in message
news:2m********************************@4ax.com.. .
Can I do this in one SQL statement? If so, please tell me how. I
have wasted a ton of time on this but there has to be an easy way to
do it. I should have done it with more than one statement and been
done with it but I can't stop thinkin about it.

Table:
Grades

Columns:
StudentID
GradeDate
Grade

I want to get the last grade for each student. However, the maximum
date for each student is different. I tried

SELECT max(GradeDate), StudentID, Grade
FROM Grades;

Didn't give me what I wanted.

Please tell me how to get the newest grade for each student.

Bill


Bill, like most all of this stuff, there's more than one way to do it.
There's probably better ways, as well, but this is what I came up with:

SELECT StudentID, GradeDate, Grade
FROM Grades INNER JOIN
(SELECT Max(GradeDate) AS MaxDate, StudentID
FROM Grades GROUP BY StudentID) as g
ON (Grades.StudentID=g.StudentID) AND (Grades.GradeDate = g.MaxDate);

HTH


Thank you.

That's just what I needed.
Dec 23 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try this:

SELECT StudentID, Grade
FROM Grades As G
WHERE GradeDate = (SELECT MAX(GradeDate) FROM Grades
WHERE StudentID = G.StudentID)

This is an example of a correlated subquery. The subquery retrieves the
latest (max) GradeDate for each StudentID in the main query.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ7HC1IechKqOuFEgEQJQ4ACg25vXisU7IWbeTpAKW8uc7/d3CkEAoL2A
yEt2NA3/1rod2N8iq8ZCQq/C
=iYDC
-----END PGP SIGNATURE-----

Bill wrote:
Can I do this in one SQL statement? If so, please tell me how. I
have wasted a ton of time on this but there has to be an easy way to
do it. I should have done it with more than one statement and been
done with it but I can't stop thinkin about it.

Table:
Grades

Columns:
StudentID
GradeDate
Grade

I want to get the last grade for each student. However, the maximum
date for each student is different. I tried

SELECT max(GradeDate), StudentID, Grade
FROM Grades;

Didn't give me what I wanted.

Please tell me how to get the newest grade for each student.

Bill

Dec 27 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
3
by: MUHAMAMD SALIM SHAHZAD | last post by:
dear respected sir, i would like to ask you following problem to anyone can give me hints - i have database of insurance company, where policy table is link to customer table and each policy...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
10
by: Ryan Dahl | last post by:
Hi, I'm working on a simple performance-program, where I need to extract information from the 2 newest periods for every performance-indicator - And from there calculate a trend between these...
3
by: bluez | last post by:
I want to design a webpage where user can search the data from the database and list out the related records. Each of the record got a delete button which allow user to delete the record. ...
18
by: Drayno241 | last post by:
I'm working in access 2002. I have three tables : 1- District Data (Student ID, name, grade, etc) 2- Rosters (RRec ID,Campus, Teacher ID) 3- Students on Roster(SRec ID, RRec ID, Student ID) ...
7
by: R. Rajesh Jeba Anbiah | last post by:
What would be the ideal HTML markup for the following list of records (except table)? Student-1: Marks: Math : 100 Science : 100 History : 100 Student-2:
0
by: solargovind | last post by:
Hello, I have few problem with Dlookup condition. I need to retrieve next record or previous record based on certain condition. The conditions are set in in the combo box. Here, I am trying to...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.