473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3590
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.