Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Show most recent record in GROUP BY?

Tags:

I have a table that looks like this:

id    |    SubjectCode    |    Grade    |    DateApproved    |    StudentId  1            SUB123            1.25            1/4/2012            2012-12345  2            SUB123            2.00            1/5/2012            2012-12345  3            SUB123            3.00            1/5/2012            2012-98765    

I'm trying to GROUP BY SubjectCode but i'd like it to display the most recent DateApproved so it will look like:

  id    |    SubjectCode    |    Grade    |    DateApproved    |    StudentId  2            SUB123            2.00            1/5/2012            2012-12345  3            SUB123            3.00            1/5/2012            2012-98765   

I'm a little bit lost on how to do it?

EDIT:

Ok guys now im on my real PC, sorry for the poorly constructed question.

Here's what I'm actually trying to do:

SELECT GD.GradebookDetailId, G.SubjectCode, G.Description, G.UnitsAcademic, G.UnitsNonAcademic,  GD.Grade, GD.Remarks, G.FacultyName, STR_TO_DATE(G.DateApproved, '%m/%d/%Y %h:%i:%s') AS 'DateAproved' FROM gradebookdetail GD INNER JOIN gradebook G ON GD.GradebookId=G.GradebookId  WHERE G.DateApproved IS NOT NULL AND G.GradebookType='final' AND StudentIdNumber='2012-12345'  GROUP BY <?????> ORDER BY G.SubjectCode ASC 

Basically, I only want to display the most recent "DateApprove" of a "SubjectCode", so I don't get multiple entries.

like image 367
A.B. User Avatar asked May 04 '12 08:05

A.B. User


People also ask

How do you group by and get the latest record in SQL?

Retrieving the last record in each group using GROUP BY There are two solutions explained here using the GROUP BY clause. In both these solutions, we will be using the MAX() function to get the maximum value of id and then retrieving the other columns corresponding to this maximum id.

How do I get last record by group by?

The group by will always return the first record in the group on the result set. SELECT id, category_id, post_title FROM posts WHERE id IN ( SELECT MAX(id) FROM posts GROUP BY category_id ); This will return the posts with the highest IDs in each group.

How do I show most recent records in SQL?

Here is the syntax that we can use to get the latest date records in SQL Server. Select column_name, .. From table_name Order By date_column Desc; Now, let's use the given syntax to select the last 10 records from our sample table.


1 Answers

Start with this:

select StudentId, max(DateApproved)  from tbl group by StudentId 

Then integrate that to main query:

select *  from tbl where (StudentId, DateApproved) in  (   select StudentId, max(DateApproved)    from tbl   group by StudentId ) 

You can also use this:

select *  from tbl join (select StudentId, max(DateApproved) as DateApproved        from tbl        group by StudentId) using (StudentId, DateApproved) 

But I prefer tuple testing, it's way neater

Live test: http://www.sqlfiddle.com/#!2/771b8/5

like image 122
Michael Buen Avatar answered Sep 20 '22 12:09

Michael Buen