Ranking combination of columns with rank using Dense_Rank in SQL Server

12,199

I think you want this (SQLFiddle supplied by JW):

SELECT StageID, DepartmentNumber, UserEmail
        ,DENSE_RANK() OVER (PARTITION BY DepartmentNumber
         ORDER BY UserEmail ASC) AS DRANK 
   FROM mytable 

Protip - You never want the same columns in both the PARTITION BY and ORDER BY clauses.

This is if I've understood your requirements. I think you want each departments rows to be ranked independently, and the column to select ranks as the email. If that's not your requirement, you need to be more explicit than

I want the combination of DepartmentNumber and UserEmail to be considered when ranking the records.

Share:
12,199
user1138780
Author by

user1138780

Updated on June 14, 2022

Comments