Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Ranking issue

Author  Topic 

tariq2
Posting Yak Master

125 Posts

Posted - 2011-08-02 : 12:39:34
Hello, thank you in advance for your help.
The following query does not quite produce the desired results.
I want a ranking by username (in descending order) of reports used. So the rank colun shiould be 1,2,3,4....
Currently it shows 1,1,1,1,1....
What am I doing wrong? Thank you


select UserName,SUM(reportcount) as rc,rank() over (partition by username order by sum(reportcount)desc) as 'rank' from View_BIUsers_ReportData where TreeYear = '2011'
and reportname = 'AOT' AND YEAR = '2011' group by UserName order by rc desc

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-08-02 : 12:48:44
quote:
Originally posted by tariq2

Hello, thank you in advance for your help.
The following query does not quite produce the desired results.
I want a ranking by username (in descending order) of reports used. So the rank colun shiould be 1,2,3,4....
Currently it shows 1,1,1,1,1....
What am I doing wrong? Thank you


select UserName,SUM(reportcount) as rc,rank() over (partition by username order by sum(reportcount)desc) as 'rank' from View_BIUsers_ReportData where TreeYear = '2011'
and reportname = 'AOT' AND YEAR = '2011' group by UserName order by rc desc


Remove the partition by clause:
SELECT
UserName,
SUM(reportcount) AS rc,
RANK() OVER(PARTITION BY username ORDER BY SUM(reportcount)DESC) AS 'rank'
FROM
View_BIUsers_ReportData
WHERE
TreeYear = '2011'
AND reportname = 'AOT'
AND YEAR = '2011'
GROUP BY
UserName
ORDER BY
rc DESC
Go to Top of Page

tariq2
Posting Yak Master

125 Posts

Posted - 2011-08-02 : 13:28:33
Thank you Sunita, works perfectly
Go to Top of Page
   

- Advertisement -