Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count the TOP n Records

Posted on 2011-03-03
4
Medium Priority
?
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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