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 |
|
jed100n
Starting Member
3 Posts |
Posted - 2004-07-19 : 11:45:28
|
| Hi there,I'm having real trouble with this one.I run a golf site whereby the league table is ordered in descending points order from a sum of the top 16 weeks worth of points from each member. There are more than 16 weeks in a season but the sum and therefore the order is only displayed on a particular members top 16 scores throughout the season.I have a query which works for finding the top 16 points for an individual member as follows:SELECT SUM(Points) AS t16, memnameFROM (SELECT TOP 16 Points, memname FROM points WHERE memname = 'dashin' ORDER BY points DESC) DERIVEDTBLGROUP BY memnameORDER BY SUM(Points) DESCBut when I take the where memname = 'membername' out in order to display a full league table (of the sum of all members top 16 points), it returns an obscure set of results which are no good to me.Any help would be really appreciated.Thanks,Mike. |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-19 : 12:44:51
|
| Create Table #points (memname nvarchar(25), points int)Insert Into #pointsSelect 'Corey', 75 Union AllSelect 'Corey', 79 Union AllSelect 'Corey', 80 Union AllSelect 'Corey', 77 Union AllSelect 'Corey', 90 Union AllSelect 'Corey', 115 Union AllSelect 'Corey', 89 Union AllSelect 'Jacob', 78 Union AllSelect 'Jacob', 74 Union AllSelect 'Jacob', 83 Union AllSelect 'Jacob', 87 Union AllSelect 'Jacob', 108 Union AllSelect 'Jacob', 104 Union AllSelect 'Jacob', 92--top 5 becuase I'm lazySelect top 5 memname, points from #points Where memname = 'Corey'--top 5 againSelect * From ( Select memname, points, rank = (Select count(Distinct points) From #points Where memname = A.memname and points <= A.points) from #points as A-- Where memname = 'Corey' ) as ZWhere rank <= 5Order By memname, RankInsert Into #points Select memname = 'Corey', points = 79--and againSelect * From ( Select memname, points, rank = (Select count(Distinct points) From #points Where memname = A.memname and points <= A.points) from #points as A-- Where memname = 'Corey' ) as ZWhere rank <= 5Order By memname, RankDrop Table #pointsCorey |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-07-19 : 13:24:02
|
| I woulnd't use Distinct in the counts; though -- other wise if you have:10102030the rank of 30 would be 3rd -- when it really should be 4th.- Jeff |
 |
|
|
jed100n
Starting Member
3 Posts |
Posted - 2004-07-19 : 13:28:43
|
| Thanks Corey,I'm a total SQL novice and I hope i've not misunderstood your kind response but my problem is this...Here is a simplified table of where I get my data from initially:Name.Points.Week-------------------Roger...12.....1Tom.....11.....1Mike....10.....1Alan.....9.....1Roger...12.....2Alan....11.....2Mike....10.....2Tom......9.....2Alan....12.....3Mike....11.....3Roger...10.....3Tom......9.....3So assuming I was working to the best top 2 results per member, my league table would look like this:1. Roger (12+12) = 242. Alan (12+11) = 233. Mike (11+10) = 214. Tom (11+9) = 20I just need to be able to return the memname and points in their respective descending orders.Regards,Mike. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-07-19 : 13:52:27
|
| Create Table #points (memname nvarchar(25), points int)Insert Into #pointsSelect 'Corey', 75 Union AllSelect 'Corey', 77 Union AllSelect 'Corey', 80 Union AllSelect 'Corey', 79 Union AllSelect 'Corey', 90 Union AllSelect 'Corey', 115 Union AllSelect 'Corey', 89 Union AllSelect 'Jacob', 78 Union AllSelect 'Jacob', 77 Union AllSelect 'Jacob', 83 Union AllSelect 'Jacob', 87 Union AllSelect 'Jacob', 108 Union AllSelect 'Jacob', 104 Union AllSelect 'Jacob', 92--top 2Select * From(Select memname, points, rank = (Select count(points) From #points Where memname = A.memname and points >= A.points) from #points as A) as ZWhere rank <= 2Order By memname, RankSelect memname, points = sum(points) From(Select memname, points, rank = (Select count(points) From #points Where memname = A.memname and points >= A.points) from #points as A) as ZWhere rank <= 2Group By memnameOrder By points DescDrop Table #pointsCorey |
 |
|
|
jed100n
Starting Member
3 Posts |
Posted - 2004-07-20 : 05:52:21
|
| Pure genius, thank you Seventhnight - saved the day!Mike. |
 |
|
|
|
|
|
|
|