For example, N is 10 and a table looks like
id 1 2 3 4 5 6 7 10 11 12 13 108 109 111 112 113
Need to find first N rows that have unique value with mod(id, N).
Expected result is
mod10 1 2 3 4 5 6 7 10 108 109
I’ve tried something like
select * from ( select id, id % 10 as seq_id from accounts order by id ) as s1 group by s1.seq_id limit 10;`
but not working.
Advertisement
Answer
You can use window function here –
SELECT id FROM (SELECT id, ROW_NUMBER() OVER(PARTITION BY id % 10 ORDER BY id) RN FROM table_name ) X WHERE RN = 1 ORDER BY id