I am looking for some assistance on a query.
I have a Stored Procedure where I am attemtping to step through the results and return every other nth record..
Here is what I have so far:
Expand|Select|Wrap|Line Numbers
- SELECT * FROM (SELECT TOP(' + @SampleAmount + ') * FROM (SELECT DISTINCT TOP(' + @TotalRecords + ') * FROM ' + @TableName + ' WHERE CountyNumber = ' + @CountyNumber + ' ORDER BY ID ASC) As Foo ORDER BY ID DESC) As Bar ORDER BY ID ASC'
@TotalRecords is the total amount of records available
@TableName is duh, the table from which the recrods come
@CountyNumber is the county number to filter the results
This works fantastic for really large recordsets, like 10,000.. But i am seeing some inconsistancies when trying to step through a smaller amount.
Say There are only 700 TotalRecords availale, and I want 100 records returrned, with the above SP i woudl only get 7 records..If i wanted 40 records I would get 17 i think, and i think they exact cutoff where i would get exactly what i request is 26 records..
There is obviuously a flaw in my logic, can anyone assist in this??
Thanks in advance for any help!