Solved

Get highest ranked rows from SQL table.

Posted on 2013-01-28
5
355 Views
Last Modified: 2013-01-31
Say I have an Oracle table with the following in it...

CLASS,YEAR,TEACHER,GRADE,STUDENT
1A,2009,Mr A,1.1,Miss B
1A,2009,Mr A,1.2,Miss B
1A,2009,Mr B,1.3,Miss B
1A,2009,Mr A,1.9,Miss C
1A,2009,Mr A,2.0,Miss C
1A,2009,Mr B,2.1,Miss C

How can I only return one row for each student (Miss B, Miss C) but make sure that row is the one that corresponds to the highest grade value, so out of the above I would want only...

CLASS,YEAR,TEACHER,GRADE,STUDENT
1A,2009,Mr B,1.3,Miss B
1A,2009,Mr B,2.1,Miss C

I am sure this is an easy thing to do? Oracle syntax would be good please!!
0
Comment
Question by:Blowfelt82
5 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38826470
select * from table
where (student, grade) in (select student, max(grade) from table group by student)
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38826500
0
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38826683
I guess you could use the DENSE_RANK analytic function. Something like:

SELECT CLASS,YEAR,TEACHER,GRADE,STUDENT,
       DENSE_RANK() OVER (PARTITION BY STUDENT ORDER BY Grade DESC) "rank"
FROM   table_name

You can use as a subquery and then select the rows where rank = 1.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 38826689
This should work as well.
SELECT * 
FROM   (SELECT class, 
               year, 
               teacher, 
               grade, 
               student, 
               Row_number() 
                 OVER ( 
                   partition BY student 
                   ORDER BY grade DESC) rn 
        FROM   grades) 
WHERE  rn = 1; 

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 38826971
If a student took more than one class, would you still only want one row for that student with the highest grade regardless of the class, or want one row for each class the student took with the highest grade for each class?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now