Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get highest ranked rows from SQL table.

Posted on 2013-01-28
5
Medium Priority
?
368 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 38

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 2000 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

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

715 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