title: How can I reuse a Common Table Expression
body: <p>I am using a Common Table Expression for paging:</p>
  
  <pre><code>with query as (
    Select Row_Number() over (Order By OrderNum ASC) as TableRowNum,
           FirstName,
           LastName
    From   Users
  )
  Select * from query where TableRowNum between 1 and 25 Order By TableRowNum ASC
  </code></pre>
  
  <p>Immediately after making this query, I make make an almost identical query in order to retrieve the total number of items:</p>
  
  <pre><code>with query as (
    Select Row_Number() over (Order By OrderNum ASC) as TableRowNum,
           FirstName,
           LastName
    From   Users
  )
  Select Count(*) from query
  </code></pre>
  
  <p>I have tried combining these together (ie: define the CTE, query the data and then query the Count, but when I do this, I get an error message "Invalid object name 'query'" in response the the second query (the Count).</p>
  
  <p>Is there any way to combine these two queries into one, to save a round-trip to the DB?</p>