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.
| Author |
Topic |
|
empyrean
Starting Member
14 Posts |
Posted - 2011-02-23 : 12:08:50
|
| Hi i tried many ways to solve this but missing some thing.. I have two tables Student and ScoreSid Cid Score6 1 901 1 804 1 803 1 702 1 606 2 801 2 702 2 604 2 605 2 504 3 807 3 806 3 708 3 602 3 50Sid Sname Sbday Ssex1 As 1980 female2 Al 1986 male3 An 1989 male4 ja 1986 male5 ma 1983 female6 phi 1986 male7 Geo 1993 male8 lil 1990 female9 cha 1993 maleI need to Return Sid and Sname of the students who have the top 2 highest score for each course. If existed, return Sid and Sname of the student who has the highest score among all the male students for each course.Here top 2 highest score is not just top two records in a group for ex : top 2 highest score in 1st group is 90, 80 ,80 .I need out put like thisSid Cid Score6 1 901 1 804 1 806 2 801 2 702 2 604 2 604 3 807 3 806 3 70I tried the following code :select A.Sid , S.SNAME, Score,Cid from Score a,STUDENTS S where 2 >(select count(Cid) from Score where Cid=a.Cid and Score>a.Score) AND A.SID = S.SID order by Cid,Score desc |
|
|
empyrean
Starting Member
14 Posts |
Posted - 2011-02-23 : 12:54:17
|
| got the answerSELECT SC.Sid , ST.SNAME, SC.Score, SC.Cid FROM ( SELECT *, DENSE_RANK() OVER(PARTITION BY Cid ORDER BY Score DESC) TopScore FROM Score) AS SCINNER JOIN Students AS STON SC.Sid = ST.SidWHERE SC.TopScore <= 2 |
 |
|
|
|
|
|