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)
 Get Rank ID's based on RankPoints

Author  Topic 

skylimit
Starting Member

4 Posts

Posted - 2009-08-06 : 12:48:53
Hi All,
This is my first post and know you guys would definitely help me as I got stuck to get results.

Sample data
RankPoints PersonName
100 Jon
100 Sam
200 Andy
201 July
201 Tony
10 Smith

I would like to have two expected results
First Result I want
RankID RankPoints PersonName
1 201 July
1 201 Tony
3 200 Andy
4 100 Jon
4 100 Sam
6 10 Smith

Second Result I want
RankID RankPoints PersonName
1 201 July
1 201 Tony
2 200 Andy
3 100 Jon
3 100 Sam
4 10 Smith

Thanks a lot for reading this and helping me out.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-08-06 : 13:27:47
USE Rank() for the first and Dense_Rank for the second

Jim
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-06 : 13:30:16
yes - I'll post anyway :)


declare @t table (RankPoints int, PersonName varchar(10))
insert @t
select 100, 'Jon' union all
select 100, 'Sam' union all
select 200, 'Andy' union all
select 201, 'July' union all
select 201, 'Tony' union all
select 10, 'Smith'

select rank() over (order by rankPoints desc) as RankID
,RankPoints
,PersonName
from @t

select dense_rank() over (order by rankPoints desc) as RankID
,RankPoints
,PersonName
from @t

OUTPUT:
RankID RankPoints PersonName
-------------------- ----------- ----------
1 201 July
1 201 Tony
3 200 Andy
4 100 Jon
4 100 Sam
6 10 Smith


RankID RankPoints PersonName
-------------------- ----------- ----------
1 201 July
1 201 Tony
2 200 Andy
3 100 Jon
3 100 Sam
4 10 Smith


Be One with the Optimizer
TG
Go to Top of Page

skylimit
Starting Member

4 Posts

Posted - 2009-08-06 : 13:42:53
Thanks Guys- Especially TG
What if I don't have sql server 2005? Any other way around?
Thanks
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-06 : 13:50:25
Then you should have posted the question in the appropiate forum

What version are you using?

Be One with the Optimizer
TG
Go to Top of Page

skylimit
Starting Member

4 Posts

Posted - 2009-08-06 : 14:05:47
SQL Server 2000
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-06 : 14:15:09
Then maybe this:

declare @t table (RankPoints int, PersonName varchar(10))
insert @t
select 100, 'Jon' union all
select 100, 'Sam' union all
select 200, 'Andy' union all
select 201, 'July' union all
select 201, 'Tony' union all
select 10, 'Smith'

select (select count(*) from @t where rankPoints > t.rankPoints) + 1 as RankID
,RankPoints
,PersonName
from @t t
order by 1

select (select count(distinct RankPoints) from @t where rankPoints > t.rankPoints) + 1 as RankID
,RankPoints
,PersonName
from @t t
order by 1

OUTPUT:
RankID RankPoints PersonName
----------- ----------- ----------
1 201 July
1 201 Tony
3 200 Andy
4 100 Jon
4 100 Sam
6 10 Smith


RankID RankPoints PersonName
----------- ----------- ----------
1 201 July
1 201 Tony
2 200 Andy
3 100 Jon
3 100 Sam
4 10 Smith


Be One with the Optimizer
TG
Go to Top of Page

skylimit
Starting Member

4 Posts

Posted - 2009-08-06 : 14:20:59
Thanks mate you are a genius.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-07 : 01:28:49
You can also play with "Quirky update" method
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-07 : 10:00:08
quote:
Originally posted by madhivanan

You can also play with "Quirky update" method
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx

Madhivanan

Failing to plan is Planning to fail



Yes, this method: update <table> set <variable> = <column> = <expression>

With large amounts of data that is definately the fastest method I'm aware of. But be sure to read Jeff Moden's article(s) on this method. He's "the man" when it comes this method becuase he as done a lot of research and testing. And you will need to adjust your code depending on what sql version you're running. Guaranteeing the order of the updates is obviously critical and he has worked that out.

Be One with the Optimizer
TG
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-07 : 10:41:50
quote:
Originally posted by TG

quote:
Originally posted by madhivanan

You can also play with "Quirky update" method
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspx

Madhivanan

Failing to plan is Planning to fail



Yes, this method: update <table> set <variable> = <column> = <expression>

With large amounts of data that is definately the fastest method I'm aware of. But be sure to read Jeff Moden's article(s) on this method. He's "the man" when it comes this method becuase he as done a lot of research and testing. And you will need to adjust your code depending on what sql version you're running. Guaranteeing the order of the updates is obviously critical and he has worked that out.

Be One with the Optimizer
TG


Ok. I see his post now and he tells having clustered index as part of the update gaurantees the order
http://www.sqlservercentral.com/Forums/Topic449802-203-1.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -