SQL Server - Row Number - Asked By chaz d'chaz on 11-Feb-15 10:30 AM

All I want to do is establish a Rank by Totals.  I know there's a "Rank" function but RowNumber() does what I need (I think) and seems simpler: that is, it has fewer confusing disclaimers in the documentation.  As it is, there are still a couple, so I thought I'd submit the query here and ask, is this apt to do something weird and counterintuitive?  Because as written, it works fine -- it orders by Total Desc, and then assigns row numbers
 
SELECT [Name]
    ,[Total]
    ,ROW_NUMBER() over (order by Total desc) as "Rank"
  FROM [theDB].[dbo].[theQuery]
  order by Total desc;

This works, too...

SELECT [Name]
    ,[Total]
    ,ROW_NUMBER() over (order by Total desc) as "Rank"
  FROM [theDB].[dbo].[theQuery]
 


...so I guess that means that "over(order by Total desc)" orders the result set, and then RowNumber() is applied.  ?

Robbe Morris replied to chaz d'chaz on 11-Feb-15 10:31 AM
I checked the execution plan and they both seem to function the same way.  Not exactly sure what the query optimizer does under the hood with a query like this.
chaz d'chaz replied to Robbe Morris on 11-Feb-15 01:04 PM
Ah, the "execution plan", eh?  I'm going to have to check that out.

Thx, Robbe.
Robbe Morris replied to chaz d'chaz on 11-Feb-15 03:46 PM
Menu option in SQL Server Management Studio
chaz d'chaz replied to Robbe Morris on 12-Feb-15 08:36 AM
xlent. Thx.