| 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 PersonName100 Jon100 Sam200 Andy201 July201 Tony10 SmithI would like to have two expected resultsFirst Result I want RankID RankPoints PersonName 1 201 July 1 201 Tony 3 200 Andy 4 100 Jon 4 100 Sam 6 10 SmithSecond Result I want RankID RankPoints PersonName 1 201 July 1 201 Tony 2 200 Andy 3 100 Jon 3 100 Sam 4 10 SmithThanks 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 secondJim |
 |
|
|
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 @tselect 100, 'Jon' union allselect 100, 'Sam' union allselect 200, 'Andy' union allselect 201, 'July' union allselect 201, 'Tony' union allselect 10, 'Smith'select rank() over (order by rankPoints desc) as RankID ,RankPoints ,PersonName from @tselect dense_rank() over (order by rankPoints desc) as RankID ,RankPoints ,PersonName from @tOUTPUT:RankID RankPoints PersonName-------------------- ----------- ----------1 201 July1 201 Tony3 200 Andy4 100 Jon4 100 Sam6 10 SmithRankID RankPoints PersonName-------------------- ----------- ----------1 201 July1 201 Tony2 200 Andy3 100 Jon3 100 Sam4 10 Smith Be One with the OptimizerTG |
 |
|
|
skylimit
Starting Member
4 Posts |
Posted - 2009-08-06 : 13:42:53
|
| Thanks Guys- Especially TGWhat if I don't have sql server 2005? Any other way around?Thanks |
 |
|
|
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 OptimizerTG |
 |
|
|
skylimit
Starting Member
4 Posts |
Posted - 2009-08-06 : 14:05:47
|
| SQL Server 2000 |
 |
|
|
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 @tselect 100, 'Jon' union allselect 100, 'Sam' union allselect 200, 'Andy' union allselect 201, 'July' union allselect 201, 'Tony' union allselect 10, 'Smith'select (select count(*) from @t where rankPoints > t.rankPoints) + 1 as RankID ,RankPoints ,PersonNamefrom @t torder by 1select (select count(distinct RankPoints) from @t where rankPoints > t.rankPoints) + 1 as RankID ,RankPoints ,PersonNamefrom @t torder by 1OUTPUT:RankID RankPoints PersonName----------- ----------- ----------1 201 July1 201 Tony3 200 Andy4 100 Jon4 100 Sam6 10 SmithRankID RankPoints PersonName----------- ----------- ----------1 201 July1 201 Tony2 200 Andy3 100 Jon3 100 Sam4 10 Smith Be One with the OptimizerTG |
 |
|
|
skylimit
Starting Member
4 Posts |
Posted - 2009-08-06 : 14:20:59
|
| Thanks mate you are a genius. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
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" methodhttp://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspxMadhivananFailing 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 OptimizerTG |
 |
|
|
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" methodhttp://sqlblogcasts.com/blogs/madhivanan/archive/2009/06/10/quirky-update-in-sql-server.aspxMadhivananFailing 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 OptimizerTG
Ok. I see his post now and he tells having clustered index as part of the update gaurantees the orderhttp://www.sqlservercentral.com/Forums/Topic449802-203-1.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|