Author |
Topic |
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 00:57:42
|
I am using rank() function in SQL. I want to know, how to break the tie in SQL server 2005.If anybody knows, please reply |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 01:01:07
|
didnt get that. can you explain? do you want to give them seperate rank. then use ROW_NUMBER(). if you want to use continuos ranks use DENSE_RANK |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 01:03:33
|
For example I am doing rank for Salary. If two persons salary is equal then i need to do the rank based on the another field HierarchyID(like 1,2,3 in my requirement). |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 01:06:14
|
quote: Originally posted by Govilakshmi For example I am doing rank for Salary. If two persons salary is equal then i need to do the rank based on the another field HierarchyID(like 1,2,3 in my requirement).
then what you want is ROW_NUMBER() with PARTITION BY. look for syntax and usage in books online. |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 01:18:35
|
As per my understanding the Row_Number and Partition will reset the continous rank.How it will help in this scenario? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 01:35:35
|
quote: Originally posted by Govilakshmi As per my understanding the Row_Number and Partition will reset the continous rank.How it will help in this scenario?
show your current sample data and rank you expect it to get |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 01:54:49
|
The rank its showing currently is Value Rank582 43534 316707 116707 1 6917 2But i have Hierarchy id in another table. So, if tie is happened then i need to calcualate the rank based on the Hierarchy. For example, if the hierarchy id is 1 then the rank should be high, if the hierarchy is 2 then the rank should be lesser |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 02:02:58
|
quote: Originally posted by Govilakshmi The rank its showing currently is Value Rank582 43534 316707 116707 1 6917 2But i have Hierarchy id in another table. So, if tie is happened then i need to calcualate the rank based on the Hierarchy. For example, if the hierarchy id is 1 then the rank should be high, if the hierarchy is 2 then the rank should be lesser
ok then what you can add hierarchyid also in order by clause of rank function. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 02:18:50
|
as an example:-declare @table table(val numeric(10,2),hid int)insert into @tableselect 111.22,1 union allselect 65.42,1 union allselect 13.20,1 union allselect 105.22,1 union allselect 105.22,2 union allselect 100.02,1 union allselect 125.12,2 union allselect 110.00,1 select rank() over (order by val,hid) as ran,*from @tableoutput---------------ran val hid-------------------- --------------------------------------- -----------1 13.20 12 65.42 13 100.02 14 105.22 15 105.22 26 110.00 17 111.22 18 125.12 2 |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 02:46:25
|
Thanks visak I have to use Hid only when the rank is in tie. Otherwise, no need to calculate rank based on the Hid. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 02:52:51
|
can you give some sample data to illustrate this. didnt get what you're asking for? |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-19 : 02:58:48
|
For example rank is showing like followingName Value RankA 582 4B 3534 3C 16707 1D 16707 1 E 6917 2In another table i am having Hierarchy for these nameName HierarchyA 4B 3C 1D 2E 5Now, what i want is if the value is in tie(In our example the names C and D is in tie). So, i want to check the hierarchy in another table. If the hierarchy is Higher than the rank also should high. The expected ourput isName Value RankA 582 5B 3534 4C 16707 1D 16707 2 E 6917 3Can u please give me the solution |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 03:15:32
|
[code]declare @test table(Name char(1),Value int, [Rank] int)insert into @test (Name,Value)select 'A', 582 union allselect 'B', 3534 union allselect 'C', 16707 union allselect 'D', 16707 union allselect 'E', 6917 declare @test1 table(Name char(1),Hierarchy int)insert into @test1select 'A', 4 union allselect 'B', 3 union allselect 'C', 1 union allselect 'D', 2 union allselect 'E', 5select t.Name,t.Value,rank() over(order by t.Value desc,t1.Hierarchy) as RnkFROm @test tjoin @test1 t1on t1.Name=t.Nameorder by t.Nameoutput---------------------Name Value Rnk---- ----------- ----A 582 5B 3534 4C 16707 1D 16707 2E 6917 3[/code] |
 |
|
Govilakshmi
Starting Member
8 Posts |
Posted - 2008-08-20 : 07:18:58
|
Thanks a lot visak,Its working fine. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-20 : 07:43:09
|
quote: Originally posted by Govilakshmi Thanks a lot visak,Its working fine.
you're welcome |
 |
|
|