i have a table like
ID | Name | Rank
1 Name1
2 Name2
1 Name3
2 Name4
3 Name5
1 Name6
1 Name7
My result should be this:
ID | Name | Rank
1 Name1 1
2 Name2 1
1 Name3 2
2 Name4 2
3 Name5 1
1 Name6 3
1 Name7 4
I found this: Rank over(order by name) but it will rank the whole table. How do I do a rank grouped by ID?
UPDATE:
I’m sorry guys. Michael Ames provided perfectly right answer. But I need the same thing with WHERE condition. I misinterpreted the question and I’m sorry. The real question is this:
So I need to rank all names where Client is not null order by Name Group by ID.
I have these tables:
ID | Name | Client | Rank
1 Name1 Client1
2 Name2 NULL
1 Name3 Client2
2 Name4 Client3
3 Name5 Client4
1 Name6 NULL
1 Name7 Client5
My result should be this:
ID | Name | Client | Rank
1 Name1 Client1 1
2 Name2 NULL
1 Name3 Client2 2
2 Name4 Client3 1
3 Name5 Client4 1
1 Name6 NULL
1 Name7 Client5 3
Easy peasy:
The following example ranks each name, grouped by their ID:
The following example ranks each name, grouped by their ID, but skipping records where Client is null:
And finally, the following example uses the results from the previous example to update the table with the appropriate ranks:
On the last example, if Name is not unique, you’ll very likely want to change the WHERE clause to match on a unique identifier.
Some reasonably comprehensible documentation here: http://msdn.microsoft.com/en-us/library/ms176102.aspx
Good luck!