Zeckal
Zeckal

Reputation: 145

SQL Server: how to execute TOP before Count in a single query

I'm trying to sort through my fn_dblog. I want to get a percent ratio on how often each operation type is being executed.

The data is way too big to run a count on the entire dataset.

In one query, how to I get the top 1,000,000 rows, and then count the number of times each type of Operation appears?

Upvotes: 1

Views: 74

Answers (2)

Paul Williams
Paul Williams

Reputation: 17020

You can do a sub-select of the number of rows you want and then count grouped on the Operation column:

select Operation, count(*) as OpCount
from
(
    select top 1000000 Operation
    from MyTable
) a
group by Operation

Upvotes: 0

Hogan
Hogan

Reputation: 70513

This seems simple...

     SELECT COUNT(bar)
     FROM (
        SELECT TOP 1000000 *
        FROM FOO
     ) AS SUBQUERYRESULT
     GROUP BY bar

Upvotes: 2

Related Questions