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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Select Sum of Top n records

Author  Topic 

taylo
Yak Posting Veteran

82 Posts

Posted - 2004-07-08 : 15:34:10
Greetings

I am trying to do this in a Stroed Proc:

Select @points = Sum(Top 3 Points) from MyScores where UserID = @userid order by points desc

But it throws an error. I am not sure how to tackle this.
A user could have 10 point entries and i only want the sum of the Top 3. Does anyone know how to do this without using a temp table?

Thanks.....Rob

LarsG
Constraint Violating Yak Guru

284 Posts

Posted - 2004-07-08 : 15:38:29
[code]
Select @points = Sum(points)
from (
select Top 3 Points
from MyScores
where UserID = @userid
order by points desc ) dt
[/code]
Go to Top of Page

taylo
Yak Posting Veteran

82 Posts

Posted - 2004-07-09 : 07:39:08
Thank you!

Rob
Go to Top of Page
   

- Advertisement -