How to reference one CTE twice?

后端 未结 5 1456
粉色の甜心
粉色の甜心 2020-12-15 17:44

I have a very fat common table expression which includes row numbers so that I can return a paged result set. I also want to return the total number of records that match th

5条回答
  •  眼角桃花
    2020-12-15 18:12

    This is the best:

    ;WITH recs AS
    (SELECT a,b,c,
          row_number() over (
                             ORDER BY id) AS RowNum,
                       row_number() over () AS RecordCount
    FROM ......)
    SELECT a,b,c,rownum,RecordCount FROM recs
    WHERE rownum BETWEEN @a AND @b
    

提交回复
热议问题