How to retrieve the total row count of a query with TOP

前端 未结 5 2082
梦如初夏
梦如初夏 2021-01-06 07:22

I have a SQL Server 2008 query

SELECT TOP 10 *
FROM T
WHERE ...
ORDER BY ...

I\'d like to get also the total number of the rows. The obious

相关标签:
5条回答
  • 2021-01-06 07:42
    SELECT     TOP (2) *,
               (SELECT COUNT(*) AS Expr1 FROM T) AS C
    FROM         T
    
    0 讨论(0)
  • 2021-01-06 07:45

    Do you want a second query?

    SELECT TOP 10
        *, foo.bar
    FROM
        T
        CROSS JOIN
        (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
    WHERE
        ...
    ORDER BY
        ...
    

    OR

    DECLARE @bar int
    SELECT @bar = COUNT(*) AS bar FROM T WHERE ...
    SELECT TOP 10
        *, @bar
    FROM
        T
        CROSS JOIN
        (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
    WHERE
        ...
    ORDER BY
        ...
    

    Or (Edit: using WITH)

    WITH cTotal AS
    (
        SELECT COUNT(*) AS bar FROM T WHERE ...)
    )
    SELECT TOP 10
        *, cTotal .bar
    FROM
        T
    WHERE
        ...
    ORDER BY
        ...
    
    0 讨论(0)
  • 2021-01-06 07:50

    Remove the ORDER BY clause from the 2nd query as well.

    0 讨论(0)
  • 2021-01-06 08:00

    No.

    SQL Server doesn't keep COUNT(*) in metadata like MyISAM, it calculates it every time.

    UPDATE: If you need an estimate, you can use statistics metadata:

    SELECT  rows
    FROM    dbo.sysindexes
    WHERE   name = @primary_key,
    

    where @primary_key is your table's primary key name.

    This will return the COUNT(*) from last statistics update.

    0 讨论(0)
  • 2021-01-06 08:07

    What is in this answer seems to work:

    https://stackoverflow.com/a/19125458/16241

    Basically you do a:

    SELECT top 100 YourColumns, TotalCount = Count(*) Over()
    From YourTable
    Where SomeValue = 32
    

    TotalCount will have the total number of rows. It is listed on each row though.

    When I tested this the query plan showed the table only being hit once.

    0 讨论(0)
提交回复
热议问题