How do you Select TOP x but still get a COUNT of the whole query?

后端 未结 2 806
清酒与你
清酒与你 2020-12-28 16:22

I\'m writing a webpage to interactively filter results based on filter criteria as it is specified by the user. I only want to return from SQL the TOP 20 rows but I want to

相关标签:
2条回答
  • 2020-12-28 16:30

    select COUNT(*) from Process_Master where dt_time=(select top 1 (dt_time) from Process_Master order by DT_TIME desc)

    0 讨论(0)
  • 2020-12-28 16:43

    You can use COUNT(*) OVER()

    SELECT TOP 20 *, 
           COUNT(*) OVER() AS TotalMatchingRows
    FROM master..spt_values
    WHERE type='P'
    ORDER BY number
    

    Doing two queries may work out more efficient however especially if you have narrower indexes that can be used in determining the matching row count but don't cover the entire SELECT list.

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