Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

Count the TOP n Records


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
vliwill
Asked:
vliwill
  • 2
1 Solution
 
MistralolCommented:

What like

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

If you have > 501 you say > 500 results?
0
 
Rajkumar GsSoftware EngineerCommented:
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
 
vliwillAuthor Commented:
Perfect thanks
0
 
Rajkumar GsSoftware EngineerCommented:
Glad to help and for the points
Raj
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now