Vijay Kumar Khatri
Vijay Kumar Khatri

Reputation: 11

Buckets in postgresql

Can any tell me how to make buckets of equal sizes in PostgreSQL. For Example I have Ids:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15.

Now I want to make 3 buckets of equal sizes so there will be 3 buckets containing 5 records each , like below:

enter image description here

Upvotes: 0

Views: 1149

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270401

I would suggest ntile():

select t.*,
       ntile(3) over (order by id) as bucket
from t;

Note: These will be as equal in size as possible, if the number of tiles does not divide the number of records.

Upvotes: 2

Related Questions