I continue a series on window functions in SQL Server:
The whole concept of ranking window functions is to assign some numeric ordering to a dataset. There are four ranking functions in SQL Server. Three of them are very similar to one another:
ROW_NUMBER()
,RANK()
,DENSE_RANK()
. The fourth one,NTILE()
, is the odd cousin of the family.Unlike aggregate window functions, all ranking window functions must have at least an
ORDER BY
clause in theOVER()
operator. The reason is that you are attempting to bring order to the chaos of your data by assigning a number based on the order in which you join.
Watch me ramble on about monotonicity and quietly admit that I learned what it was from economics, where the naming feels utterly backward (“strongly monotonic” is the “greater than or equal to” of monotonicity, whereas “weakly monotonic” is the “greater than” of monotonicity). Also, I structured this entire post so that I could get that video from The Prisoner (the good one, not the garbage one) in it.