ASP Hosting sale!
Double disk space and transfer for FREE!
Limited time offer! Act Now!

aspdev | articles | tutorials | forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to get the first 10 rows from SQL Select in SQL Server?

 
Post new topic   Reply to topic    ASPdev.org Forum Index -> SQL Programming (MS SQL Server, MS Access, MySQL)
View previous topic :: View next topic  
Author Message
Sharp



Joined: 20 Oct 2004
Posts: 6

PostPosted: Wed Nov 24, 2004 2:44 pm    Post subject: How to get the first 10 rows from SQL Select in SQL Server? Reply with quote

Hi,

I have a SQL table called Orders in my SQL Server 200 database and I'm trying to get the 10 customers with the most sales. My Statement looks like this:

Code:

SELECT CustomerID, SUM(OrderDollars) FROM Orders GROUP BY CustomerID ORDER BY SUM(OrderDollars) DESC



This SQL statement works perfectly and the only problem is that I get all customers, not just the top 10. I’ve tried to put LIMIT 10 at the end of the SQL statement (just like in MySQL), but I got an error.

Can anybody help me to get the only the 10 customers with the most sales?


Thanks,

Sharp
Back to top
View user's profile Send private message
administrator
Site Admin


Joined: 01 Oct 2004
Posts: 183

PostPosted: Thu Nov 25, 2004 3:22 pm    Post subject: Reply with quote

T-SQL (the SQL Server version of SQL) has a keyword TOP which does what you want. Just change your SQL statement to:

Code:

SELECT TOP 10 CustomerID, SUM(OrderDollars) FROM Orders GROUP BY CustomerID ORDER BY SUM(OrderDollars) DESC




Cheers,
_________________
Peter
ASP & ASP.NET Articles and Tutorials
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    ASPdev.org Forum Index -> SQL Programming (MS SQL Server, MS Access, MySQL) All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group

SQL Tutorial