Home > Mobile >  SQL select skip n values every n values
SQL select skip n values every n values

Time:10-21

I have a problem and no idea how to solve it. So, a user selects how many values he wants, then I check how many values can be returned (limited by some WHERE properties). Lets say there are 15 values and the user only wants 10.

So I want 2 out of 3 values in this example, how can I create a select that skips every third value? The solution should also work with something like 5 out of 8 values.

I already searched through all solutions, mostly working with modulus and row_number or id. And while I could manually build some selects to work for certain scenarios, I lack the knowledge to make something that works just by dynamically changing some values, depending on the selected numbers.

All help is much appreciated.

EDIT

My reasoning for skipping entries, instead of selecting the first / skipping the first / randomizing and then skipping:

The entries are actually positions, skipping entries is there to make the path rougher, I would prefer to do it directly in the Database to use the servers power, instead of doing it on the User machine, filtering out possibly hundreds of thousands of entries

CodePudding user response:

Here is an example of how to skip every third row. You can easily customize it.

select *
from
(
select *
       ,row_number() over(order by col1) as rn
from t
) t
where rn%3 != 0
col1 rn
10 1
20 2
40 4
50 5
70 7
80 8
100 10
110 11

Fiddle

  • Related