SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Display COUNT(*) but ONLY TOP 100

By Luigi Marinescu, 2009/11/20

Total article views: 1790 | Views in the last 30 days: 72

Let's say you have a result set with 5000 records but you want to display only 100 of them and in the same time to display the count of ALL records (5000).

Using next script you cand get these within ONE SINGLE STEP but not two as:
SELECT COUNT(*) FROM X
SELECT TOP 100 * FROM X

ONE STEP solution:

Let's say you have a table, ContactProperty where a Contact can have more properties:

CONTACTID PROPERTYID
1 10
1 10
2 20

So, the next script returns the top 100 UNIQUE contactidIds.

 

This is what you get:

rowno contactid
54977 4378508
54976 4378507
54975 4378495
54974 4378421
54973 43784

 

The value of RowNo from the first record represents the COUNT(*) of the whole result set, in this case I have 54977 rows but this returns only 100 of them.

This is a particular case but you can remove the "GROUP BY".

ENJOY!

Luigi

By Luigi Marinescu, 2009/11/20

Total article views: 1790 | Views in the last 30 days: 72
Your response
 
 
Related Articles
FORUM

Flat file record count inside script task

how to get Flat file record count inside script task?

FORUM

Counting records over several tables

Wanting to count the number of records in each table after a certain date

FORUM

get count of the records within specific dates

get count of the records within specific dates

FORUM

Same tables different counts

Update query produces different record count

FORUM

When resultset of count is nothing

When resultset of count returns no records

Tags
count(*)    
top 100    
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com