Paging and grouping using CTE? Given the following code, from the answer to this question, how can I adapt it for paging?  I've read that Common Table Expression is a good way to do paging, but since I implemented CTE to solve a problem that was already over my head, I really have no idea where to start in merging the two solutions.

;with cte as
(
  SELECT DISTINCT d.AppId,
    d.AppName,
    d.AppType,
    Tags = STUFF((SELECT ', ' + t.TagName
                  FROM AppTags t
                  where d.AppID = t.AppID
                  FOR XML PATH (''))
                  , 1, 1, '') 
  FROM AppDetails d
  WHERE d.AppID = '1'
)
select *
from cte
where tags like '%test1%'
I was able to do this using multiple CTEs, similar to the following:

;with cte as
(
  SELECT DISTINCT d.AppId,
    d.AppName,
    d.AppType,
    Tags = STUFF((SELECT ', ' + t.TagName
                  FROM AppTags t
                  where d.AppID = t.AppID
                  FOR XML PATH (''))
                  , 1, 1, '') 
  FROM AppDetails d
  WHERE d.AppID = '1'
),
results as (
    SELECT ROW_NUMBER() OVER (ORDER BY AppID) AS RowNumber, * FROM cte
        where tags like '%test1%'
)
select *
from results
WHERE (RowNumber &gt;  @pageStart) AND (RowNumber &lt;=  @pageStart + @pageLength)