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
 New to SQL

Author  Topic 

Rosh
Starting Member

3 Posts

Posted - 2009-07-23 : 14:52:19
Hi, I have a student table with stdno,stdname,stdmajor,stdgpa etc as columns.
I need to create a Sql statement to find : For each major ----the stdno,name and gpa of student with highest gpa.

The following stmt gives error,pls help
SELECT StdMajor,StdNo,StdFirstName,StdLastName,StdGPA
WHERE EXISTS
(Select StdMajor,MAX(stdgpa) from student
GROUP BY Stdmajor)
GROUP BY Stdmajor,,StdNo,StdFirstName,StdLastName,StdGPA

Rosh

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-07-23 : 15:01:06
[code]SELECT StdMajor,
StdNo,
StdFirstName,
StdLastName,
StdGPA
FROM (SELECT Rank()
OVER(PARTITION BY StdMajor ORDER BY StdGPA DESC) AS seq,
StdMajor,
StdNo,
StdFirstName,
StdLastName,
StdGPA
FROM student) t
WHERE t.seq = 1[/code]
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-23 : 15:08:57
SELECT st.StdMajor,st.StdNo,st.StdFirstName,st.StdLastName,st.StdGPA
from student st
inner join
(SELECT StdMajor,StdNo,max(StdGPA) as StdGPA from student group by StdMajor,StdNo)dt
on st.StdMajor=dt.StdMajor and st.StdNo=dt.StdNo and st.StdGPA = dt.StdGPA


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Rosh
Starting Member

3 Posts

Posted - 2009-07-23 : 15:26:20
Hi,

Your query didnt work.
Instead of grouping by major it gave all the rows in student table
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-07-23 : 15:28:55
Did u try mine?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-23 : 15:36:35
I think there is an easy solution!
Assuming that one StdNo has always the same first and last name, you can do this:
SELECT StdMajor,StdNo,max(StdFirstName) as StdFirstName,max(StdLastName) as StdLastName,max(StdGPA) as StdGPA
from student group by StdMajor,StdNo


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Rosh
Starting Member

3 Posts

Posted - 2009-07-23 : 16:23:38
Still doesnt work

i think we need to use subquery for this prblem
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-23 : 16:27:27
And I think you should give some sample data!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

weipublic
Starting Member

19 Posts

Posted - 2009-07-24 : 22:13:00
vijayisonly's query working pretty good.

webfred's idea is good, but we should remove 'StdNo' from the group by phase. if including 'StdNo', it likes group by everybody, so returns all the rows in the table.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-07-25 : 04:15:57
Sample data would be the easy way to see the right solution.
OP wrote: For each major ----the stdno,name and gpa of student with highest gpa.
That means to me I cannot remove 'StdNo' from grouping.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

weipublic
Starting Member

19 Posts

Posted - 2009-07-25 : 15:50:10
Hi all,

Yeah, Sample data would be the easy way to see the right solution. I’m sorry if I misunderstand the original requirements.

I think following query should return ‘the stdno,name and gpa of student with highest gpa for each major’

SELECT S.StdMajor, S.StdNo, S.StdFirstName, S.StdLastName, S.StdGPA
FROM student S
INNER JOIN
(SELECT stdmajor, MAX(stdgpa) as stdgpa FROM student
GROUP BY stdmajor ) V
ON V.stdmajor = S.stdmajor AND V.stdgpa = S.stdgpa
Go to Top of Page
   

- Advertisement -