View previous topic :: View next topic |
Author |
Message |
Sharp
Joined: 20 Oct 2004 Posts: 6
|
Posted: Wed Nov 24, 2004 2:44 pm Post subject: How to get the first 10 rows from SQL Select in SQL Server? |
|
|
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 |
|
|
administrator Site Admin
Joined: 01 Oct 2004 Posts: 183
|
Posted: Thu Nov 25, 2004 3:22 pm Post subject: |
|
|
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 |
|
|
|
|
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
|