Solved

Get highest ranked rows from SQL table.

Posted on 2013-01-28
5
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 35

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

717 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