Solved

Get highest ranked rows from SQL table.

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

734 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