My book says that -
1- Ranking functions (such as RANK(), DENSE_RANK(), ROW_NUMBER() etc.) need an OVER() clause.
So, the code below is wrong -
select *, RANK()
from [grant]
order by Amount desc
error - Incorrect syntax near 'RANK', expected 'OVER'.
If I add OVER() after RANK() in above code, i get an error again. (Error - The ranking function "RANK" must have an ORDER BY clause.)
2- Then, my books adds that "Ranking functions need ORDER BY information to appear as an argument in the OVER()".
select *, RANK() OVER(ORDER BY Amount DESC) as GrantRank
from [grant]
My questions are -
1 - Why do we need to have an OVER() clause with a ranking function ? 2 - Why do we have to remove the order by statement and put it inside the OVER() ?
The book does not explain the logic behind these things ! Please help me to understand it.
Thanks in advance.