Solved

Get highest ranked rows from SQL table.

Posted on 2013-01-28
5
362 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 37

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 143

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 32

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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