joaoavf
joaoavf

Reputation: 1383

How to group in fixed count ranges?

Let's say I have one table with two columns (student_id,grade).

student_id is an integer and grade is a decimal number.

In case I have 1000 students and want to group them ordered by grade in groups of 10 students each.

Just to be clear, this should produce 100 groups. The first group cointains the 10 highest grades and the last group contains the 10 lowest grades.

How should I do that ? Optimization is always welcome.

Thank you very much.

Joao

Upvotes: 0

Views: 157

Answers (1)

nate c
nate c

Reputation: 9005

ntile will give a ranking by an amount of buckets.

select student_id, ntile(100) over (order by grade desc) from student

Upvotes: 1

Related Questions