Solved

Count the TOP n Records

Posted on 2011-03-03
4
295 Views
Last Modified: 2012-05-11

Is there any way of doing a count using count(*) over that only counts the first x number of records?

Reason I ask is that a search may return 300,000 results but in reality I want to say if more than 500 returned just tell the user that. SQL is so slow at counting that I don't want it ot count the full 300,000.
0
Comment
Question by:vliwill
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Mistralol
ID: 35025828

What like

SELECT TOP 501 COUNT(*) FROM Table WHERE .....

If you have > 501 you say > 500 results?
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 35025857
Evenif total COUNT is greater than or equal to 500, result of this query would be 500
If count is lesser than 500, it would return real count
SELECT COUNT(*)
FROM (SELECT TOP 500 * FROM YourTable) A

Open in new window

0
 

Author Closing Comment

by:vliwill
ID: 35025964
Perfect thanks
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35025986
Glad to help and for the points
Raj
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now