How do I rank salespeople by # customers grouped by department (with ties included)?
For example, given this table, I want to create the Rank column on the
You need to do some math. I typically take advantage of the combination of a counter field and an "offset" field. You're aiming for a table which looks like this (#Customers isn't necessary, but will give you a visual that you're doing it properly):
SalesPerson Dept #Customers Ctr Offset
Bill DeptA 20 1 1
Ted DeptA 30 2 1
Jane DeptA 40 3 1
Bill DeptB 50 4 4
Mary DeptB 60 5 4
So, to give rank, you'd do [Ctr]-[Offset]+1 AS Rank
SalesPerson, Dept, Ctr, and OffsetDept and #Customers (so that they're all sorted properly)Offset to be the MIN(Ctr), grouping on DeptRank