Reputation: 23
How can I write a query in SQL Server such as rank()
but a bit different calculate.
For example rank is:
rankNumber uniqeId
1 160
2 159
3 158
4 157
5 156
5 156
7 152
8 151
8 151
10 150
I need the result like these:
rankNumber uniqeId
1 160
2 159
3 158
4 157
5 156
5 156
6 152
7 151
7 151
8 150
How can I do this? Is there such a function in SQL Server?
Upvotes: 2
Views: 160
Reputation: 26363
To expand on the DENSE_RANK
comment, the full query is short and sweet:
SELECT
DENSE_RANK() OVER (ORDER BY uniqueId DESC) AS rankNumber,
uniqueId
FROM myTable
ORDER BY rankNumber
There's a SQL Fiddle here
Upvotes: 0
Reputation: 251
SELECT DENSE_RANK() OVER (ORDER BY TotCnt DESC) AS TopCustomers, CustomerID, TotCnt
FROM (SELECT CustomerID, COUNT(*) AS TotCnt
FROM Orders Group BY CustomerID) AS Cust
OUTPUT
Upvotes: 4