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
andUserEmail
to be considered when ranking the records.