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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Using RANKfunction and gett resutls

Author  Topic 

lols
Posting Yak Master

174 Posts

Posted - 2009-03-28 : 02:55:57
I have an Enroll Table

EnrollID
AgentID
EnrollmentDate

I want to list how many Agents did how many Enrollments using the Rankfunction in SQL Server 05/08. How to do?

Output needs to be :

Position      AgentId     CountEnroll
1 62 29
2 55 22
3 34 19

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-28 : 02:59:08
select * from
( select agentid,count(EnrollID) as countEnroll,row_number() over( partition by agentid order by agentid) as rn from urtable group by agentid) t
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2009-03-28 : 03:14:22
Thanks but I need to do this using the RANK function please
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-28 : 03:16:33
[code]select agentid, countEnroll,,rank() over( partition by agentid order by countEnroll desc) as rnk from
( select agentid,count(EnrollID) as countEnroll from urtable group by agentid) t
[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-28 : 03:19:24
please note that using rank will not give you consecutive values always. suppose if you've two agents with count as 22 in above case then both of them will have rank of 2 however the next agent ( one with 19) will have a rank of 4 (as 3 agents have more count than him). so if you want continuos rank values irrespective of how many times a rank repeats, use dense_rank() instead of rank() in above solution.
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2009-03-28 : 03:26:45
Thanks Visakh..I needed this only
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-28 : 03:32:51
welcome
Fine then...
Go to Top of Page
   

- Advertisement -