| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-10-22 : 07:31:31
|
| James writes "Hello, I have a question on getting a table to a point that I can pivot it. The following is an example of the dataI haveid diag count1 42 751 49 50 1 38 222 70 482 33 27You will notice they are in descending order by count. I need the following I think to be able to cross tab it.id diag count rank1 42 75 11 49 50 21 38 22 32 70 48 12 33 27 2How do I get the rank field populated? Is there an easy way to do this? " |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-10-22 : 09:15:27
|
this should help you:declare @table table (id int, diag int, count1 int, rank int)insert into @tableselect 1, 42, 75, null union allselect 1, 49, 50, null union all select 1, 38, 22, null union allselect 2, 70, 48, null union allselect 2, 33, 27, null union allselect 2, 30, 12, null union allselect 2, 34, 5, null union allselect 2, 54, 3, null union allselect 3, 42, 75, null union allselect 3, 49, 50, null union all select 3, 38, 22, null declare @cnt intset @cnt = 0UPDATE t1SET @cnt = rank = case when exists (select top 1 id from @table where id<t1.id) and not exists (select top 1 id from @table where id=t1.id and count1 > t1.count1) then 1 else @cnt + 1 endfrom @table t1select * from @table Go with the flow & have fun! Else fight the flow |
 |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 08:54:22
|
Hello, This works great except for one problem I did not anticipate. If the first two counts or all 3 counts are exactly the same it ranks them the same. Example the following will happen.id diag count rank1 , 10 , 1 , 11 , ab , 1 , 11 , cr , 1 , 1would like to see the rank be 1,2,3 in this case. Any suggestions?Thanks |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-29 : 09:18:56
|
really? this works fine for me...declare @table table (id int, diag int, count1 int, rank int)insert into @tableselect 1, 42, 1, null union allselect 1, 49, 1, null union all select 1, 38, 1, null union allselect 1, 33, 3, null union allselect 1, 15, 3, null union allselect 2, 70, 48, null union allselect 2, 33, 27, null union allselect 2, 30, 12, null union allselect 2, 34, 5, null union allselect 2, 54, 3, null union allselect 3, 42, 75, null union allselect 3, 49, 50, null union all select 3, 38, 22, null declare @cnt intset @cnt = 0UPDATE t1SET @cnt = rank = case when exists (select top 1 id from @table where id<t1.id) and not exists (select top 1 id from @table where id=t1.id and count1 > t1.count1) then 1 else @cnt + 1 endfrom @table t1select * from @table maybe you should be more specific. use the data in this example to show us the error??Go with the flow & have fun! Else fight the flow |
 |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 09:49:08
|
| I am not sure. but for id = 1 I got the following.id,diag,count1,rank1,42,1,11,49,1,21,38,1,31,33,3,41,15,3,5I would expect it to be flipped and rank 1 and 2 have the count of 3 and rank 3,4,5 have the count of 1.Thanks |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-29 : 09:54:30
|
emm... why??maybe it would be good if you described the correct logic behind this. it sure would be easier...Go with the flow & have fun! Else fight the flow |
 |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 10:11:08
|
| I'm sorry. I guess that would help.I have a maximum of 3 per id based on count (descending). which I am then wanting to rank those top 3 (or 2 or 1 if they only had 1 diag) by count descending. the top diagnosis's for a recipient. so if recipeint 1 had been diagnosed with a cold 25 times, the flu 10 times and step throat 1.It would be ranked 1,2,3 for that recipeint. If a recipient had 3 diagnosis but all were only 1 count. I would expect it to be 1,2,3 with no importance of which was ranked 1,2 or 3.The specific problems is as followsSelect 06300961, 03, 1, null union allselect 06300961, 02, 1, null union all select 06300961, 4, 1, null These rank 1,1,1Thanks |
 |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 10:21:30
|
| Also, does it matter that the id field is a character? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-12-29 : 10:27:45
|
no it doesn't matter. i'll take a look at it a bit later because i don't have time right now...Go with the flow & have fun! Else fight the flow |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 12:58:44
|
| Thanks Jeff but I am having trouble applying it. I have up to 3 of the same id and want to rank based on an int field with numbersThanks |
 |
|
|
jstrangeway
Starting Member
9 Posts |
Posted - 2004-12-29 : 13:51:54
|
| Thanks Jeff, Here is my result that works using your logicdeclare @table table (id int, diag int, count1 int, rank int)insert into @tableselect 1, 42, 1, null union allselect 1, 49, 1, null union all select 1, 38, 1, null union allselect 1, 33, 3, null union allselect 1, 15, 3, null union allselect 2, 70, 48, null union allselect 2, 33, 27, null union allselect 2, 30, 12, null union allselect 2, 34, 5, null union allselect 2, 54, 3, null union allselect 3, 42, 75, null union allselect 3, 49, 50, null union all select 3, 38, 22, null union allSelect 06300961, 03, 1, null union allselect 06300961, 02, 1, null union all select 06300961, 4, 1, null select *, (select count(*) from @table s2 where s1.id = s2.id and (s2.count1 > s1.count1 OR (s2.count1 = S1.count1 and S2.diag <= S1.diag))) as RankFROM @table s1 |
 |
|
|
|
|
|