Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to obtain the students'name with highest GPA

Author  Topic 

zhangrundatd
Starting Member

2 Posts

Posted - 2006-04-12 : 02:31:47
I want to obtain the top 1 students' name in each grade,but this code
dosen't work:
select top 1 name
from student
group by grade
order by GPA desc

Can anyone tells me what to do?

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-04-12 : 03:15:51
SELECT *
FROM DoYourOwnHomework
Go to Top of Page

a_r_satish
Yak Posting Veteran

84 Posts

Posted - 2006-04-12 : 03:26:15
Something like this...
create table [dbo].[stud](name varchar(5),grade char(1),gpa int)

insert into stud values('sat','a',85)
insert into stud values('bat','a',82)
insert into stud values('cat','a',78)
insert into stud values('hat','b',81)
insert into stud values('mat','b',87)
insert into stud values('pat','c',70)
insert into stud values('tat','c',78)

select Max(Name) Name,grade, Max(gpa) gpa from stud Group by grade



satish.r
"Way to success is always under Construction"
Go to Top of Page

zhangrundatd
Starting Member

2 Posts

Posted - 2006-04-12 : 03:33:51
Thanks a_r_satish a lot.
I'll have a try.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-12 : 04:15:34
Refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TheKing
Starting Member

1 Post

Posted - 2006-04-12 : 12:31:24
try this:
select Distinct Student.name, Max(grade) as T from student
group by (name) order by (T);
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-12 : 12:37:10
quote:
Originally posted by TheKing

try this:
select Distinct Student.name, Max(grade) as T from student
group by (name) order by (T);



No need to use Distinct

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -