| 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 helpSELECT StdMajor,StdNo,StdFirstName,StdLastName,StdGPA WHERE EXISTS(Select StdMajor,MAX(stdgpa) from student GROUP BY Stdmajor)GROUP BY Stdmajor,,StdNo,StdFirstName,StdLastName,StdGPARosh |
|
|
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] |
 |
|
|
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 stinner join(SELECT StdMajor,StdNo,max(StdGPA) as StdGPA from student group by StdMajor,StdNo)dton 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. |
 |
|
|
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 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-07-23 : 15:28:55
|
| Did u try mine? |
 |
|
|
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 StdGPAfrom student group by StdMajor,StdNo No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Rosh
Starting Member
3 Posts |
Posted - 2009-07-23 : 16:23:38
|
| Still doesnt worki think we need to use subquery for this prblem |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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.StdGPAFROM student S INNER JOIN (SELECT stdmajor, MAX(stdgpa) as stdgpa FROM student GROUP BY stdmajor ) VON V.stdmajor = S.stdmajor AND V.stdgpa = S.stdgpa |
 |
|
|
|