basic sql group by with percentage

Posted by David in Dakota on Stack Overflow See other posts from Stack Overflow or by David in Dakota
Published on 2010-03-15T20:13:52Z Indexed on 2010/03/15 20:19 UTC
Read the original article Hit count: 395

Filed under:
|

I have an issue and NO it is not homework, it's just a programmer who has been away from SQL for a long time having to solve a problem.

I have the following table:

create table students(
    studentid int identity(1,1), 
    [name] varchar(200), 
    [group] varchar(10), 
    grade numeric(9,2) 
)
go

The group is something arbitrary, assume it's the following "Group A", "Group B"... and so on.

The grade is on a scale of 0 - 100.

If there are 5 students in each group with grades randomly assigned, what is the best approach to getting the top 3 students (the top 80%) based on their grade?

To be more concrete if I had the following:

Ronald, Group A, 84.5
George H, Group A, 82.3
Bill, Group A, 92.0
George W, Group A, 45.5
Barack, Group A, 85.0

I'd get back Ronald, Bill, and Barack. I'd also need to do this over other groups.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql