If I do a
dense_rank() over (order by colname),
I get the same rank for all rows with the same value in the column colname.
However, I want to limit the number of rows with the same rank to @maxrows so that when @maxrows rows have the same value in colname, a new rank is assigned to the next row even if the value of colname is still the same.
How can I achieve this?
You can achieve this via using several ranking functions. We use
ROW_NUMBER()
in the middle and another column to perform tie-breaking:Here I get only up to 5 columns with each (final) rank value. The ranking is based on
type
but we useobject_id
as a secondary column to work out the order in which rows are allowed a particular rank.Turns out I'd over-complicated the above - there's no need for the first CTE and the first
DENSE_RANK
since that's effectively acting as a proxy for thetype
column in theROW_NUMBER()
function - so why not just use thetype
column directly and simplify things: