2

I am trying to select every nth row in SQL. I have been looking around Stack Overflow and piece together what I can based on my limited SQL knowledge. I have a table that has 81,225 rows. I am trying to select every 285th row in SQLite.

What I am using is as follows:

    Select *
    From Test A
    Where ROWID  > 0 AND ROWID <= 81225
    AND ROWID % 285 = 285 % 285

This query is giving me 285 rows (which i would expect), but it's not giving me the expected results. Is there something that needs to be added/changed to this code to give me the desired results?

Thank you,

Nick Johnson
  • 85
  • 3
  • 9
  • Using the `rowid` for this works only if the values are consecutive, i.e., if no rows were deleted. – CL. Mar 22 '16 at 07:51

1 Answers1

7

The condition you are using for the modulus is wrong. Use this instead:

Select *
From Test A
Where ROWID  > 0 AND ROWID <= 81225
AND ROWID % 285 = 0
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360