Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with top 10 rows in a sql query?

As a beginner in Sql Server 2005, i should get some help in getting top 10 from a table.

The scenario is like, a table Invitecount has multiple records for each userid. i have distinct userid's with the following sql query

Select distinct(userid) from inviteCount 

For each userid, i get the number of points using the following query

Select sum(points) from invitecount 
where UserID = 126429

And after i get the sum, i should have top 10 users with max points. My problem here is with writing all these statements together using arrays, etc.

IF someone could help me, i really appreciate it. Thanks in advance!

like image 747
Remo Avatar asked Mar 06 '26 17:03

Remo


1 Answers

Try this:

SELECT TOP 10 userID, SUM(Points)
FROM inviteCount
GROUP BY UserId
ORDER BY SUM(Points) desc

I'm not sure what you mean by using arrays, but this would get you the top ten userIds ordered by the sum of points.

like image 137
Abe Miessler Avatar answered Mar 08 '26 07:03

Abe Miessler