So, I know just enough SQL to be dangerous and am following an example to pull one page's worth of records from a table:
SELECT TOP #arguments.perPage# * FROM (
SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum, productdiagramparts.productdiagramid AS productdiagramid, products.id AS productid, products.title AS producttitle, totalRows = COUNT(*) OVER()
FROM manufacturers
INNER JOIN products ON manufacturers.id = products.manufacturerid
INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid
INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id
WHERE #whereClause#
) _tmpInlineView
WHERE rownum > #offset#
ORDER BY producttitle
The SELECT TOP wrapped around this of course pulls just the records for the current page. Trouble is, there are duplicates in the innermost SELECT statement I want to remove, but using DISTINCT doesn't work as shown above because the rows have already been numbered for the outer query. How can I make my innermost SELECT results distinct before numbering the rows?
Here's the solution based on the accepted answer below:
SELECT TOP #arguments.perPage# * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum, productdiagramid, productid, producttitle, totalRows = COUNT(*) OVER() FROM (
SELECT DISTINCT productdiagramparts.productdiagramid AS productdiagramid, products.id AS productid, products.title AS producttitle FROM manufacturers INNER JOIN products ON manufacturers.id = products.manufacturerid INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id WHERE #whereClause#
) _tmpDupRemove
) _tmpInlineView
WHERE rownum > #offset# ORDER BY producttitle