• It's basically the same as using the subquery but using another CTE.

    WITH tmpTable AS (

    SELECT DISTINCT *,

    ROW_NUMBER() OVER ( ORDER BY InventorySys ) AS RowNumber

    FROM dbo.Inventory

    ),

    Total AS(

    SELECT totalCount = Count(*)

    FROM tmpTable

    )

    SELECT tmp.* , tot.totalCount as RecordsCount

    FROM tmpTable tmp

    CROSS JOIN Total tot

    WHERE (tmp.RowNumber BETWEEN 1 AND 6) OR 1 = 0 OR 6 = 0

    ORDER BY tmp.RowNumber

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2