Reputation: 57
I have below requirement
I am using condition like rank() ( partition by Name order by sum(marks)) as rank
, i am getting the rank value as Rank_Coming column, but i am expecting the value like Rank expected column.
Please suggest how to achieve this
Thanks
Upvotes: 0
Views: 55
Reputation: 1464
You rank expect is not GROUP BY Name -- PARTITION BY Name
. It's ORDER BY Name
Try this:
DENSE_RANK() OVER(ORDER BY Name)
Upvotes: 1
Reputation: 31879
You need to get the SUM
first and then use that in ranking.
;WITH CteSum AS(
SELECT *,
SumMarks = SUM(Marks) OVER(PARTITION BY Name)
FROM tbl
),
CteRnk AS(
SELECT *,
rnk = DENSE_RANK() OVER(ORDER BY SumMarks DESC)
FROM CteSum
)
SELECT * FROM CteRnk
Upvotes: 1