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
 query help needed

Author  Topic 

sponguru_dba
Yak Posting Veteran

93 Posts

Posted - 2006-06-27 : 16:07:52
Hi i have table like below

SNO NAME MARKS RANK
----------------------------
1 A 56
2 B 35
3 C 77
4 D 86
5 E 35
6 F 77
7 G 63
8 H 42
9 I 51

Now i have to give ranks marks wise like this

SNO NAME MARKS RANK
----------------------------
1 A 56 5
2 B 35 8
3 C 77 2
4 D 86 1
5 E 35 8
6 F 77 2
7 G 63 4
8 H 42 7
9 I 51 6

in above table there is no 3rd and 9th Rank,because C,F shred the 2nd rank
and B and E shred the 8th rank

to 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 data
DECLARE @test TABLE (SNO TINYINT, NAME VARCHAR, MARKS TINYINT)

INSERT @test
SELECT 1, 'A', 56 UNION ALL
SELECT 2, 'B', 35 UNION ALL
SELECT 3, 'C', 77 UNION ALL
SELECT 4, 'D', 86 UNION ALL
SELECT 5, 'E', 35 UNION ALL
SELECT 6, 'F', 77 UNION ALL
SELECT 7, 'G', 63 UNION ALL
SELECT 8, 'H', 42 UNION ALL
SELECT 9, 'I', 51

-- do the work
SELECT a.*,
(SELECT 1 + COUNT(*) FROM @test b WHERE b.MARKS > a.MARKS) RANK
FROM @test a[/code]


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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 Rank

Madhivanan

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

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 a




thnks
Gaurav

Even my blood group says be -ve to all the negatives.
Go to Top of Page
   

- Advertisement -