Getting Min Max ids after splitting SQL table into n equal parts using ntile

This will split your_table into 10 equal parts, and give you the minimum and maximum id for each part:

with cte as
(
select
id,
ntile(10) over(order by a.id) as bucket_id
from your_table as a
group by a.id
)

select bucket_id, min(id), max(id)
from cte
group by bucket_id
order by bucket_id

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.