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.
| Author |
Topic |
|
sponguru_dba
Yak Posting Veteran
93 Posts |
Posted - 2006-06-27 : 16:07:52
|
| Hi i have table like belowSNO NAME MARKS RANK----------------------------1 A 56 2 B 35 3 C 77 4 D 865 E 356 F 777 G 638 H 429 I 51Now i have to give ranks marks wise like thisSNO NAME MARKS RANK----------------------------1 A 56 5 2 B 35 8 3 C 77 24 D 86 15 E 35 86 F 77 27 G 63 48 H 42 79 I 51 6in above table there is no 3rd and 9th Rank,because C,F shred the 2nd rankand B and E shred the 8th rankto generate this type of result some body help me |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-27 : 16:21:43
|
| [code]-- prepare test dataDECLARE @test TABLE (SNO TINYINT, NAME VARCHAR, MARKS TINYINT)INSERT @testSELECT 1, 'A', 56 UNION ALLSELECT 2, 'B', 35 UNION ALLSELECT 3, 'C', 77 UNION ALLSELECT 4, 'D', 86 UNION ALLSELECT 5, 'E', 35 UNION ALLSELECT 6, 'F', 77 UNION ALLSELECT 7, 'G', 63 UNION ALLSELECT 8, 'H', 42 UNION ALLSELECT 9, 'I', 51-- do the workSELECT a.*, (SELECT 1 + COUNT(*) FROM @test b WHERE b.MARKS > a.MARKS) RANKFROM @test a[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-28 : 02:07:09
|
| Where do you want to show the data?If you use front end application, do the ranking there. Trying to do in sql becomes ineffecient if there are millions of data you want to RankMadhivananFailing to plan is Planning to fail |
 |
|
|
mymatrix
Starting Member
24 Posts |
Posted - 2006-06-28 : 06:37:38
|
| Correct solution provided by Peter.You can also specify direct table name like :Tablename = Test----------------Select *, 'Rank'=1+(select count(*) from Test b where b.marks>a.marks) from Test athnksGauravEven my blood group says be -ve to all the negatives. |
 |
|
|
|
|
|
|
|