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 |
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-11 : 11:32:56
|
| -- Get all the members (ugh)DECLARE c_GetAllStreaksCurr CURSORFOR SELECT ms.MemberID, ms.dnHowLongStreak HowLong FROM MemberStreak ms ORDER BY ms.dnHowLongStreak DESCFOR READ ONLYOPEN c_GetAllStreaksCurr;BEGIN TRAN -- Get the first row and friends FETCH c_GetAllStreaksCurr INTO @lv_nMemberID, @lv_nHowLong SELECT @X = 1, @Y = @X, @lv_nPreviousHowLong = @lv_nHowLong -- Loop thru the members, and figure out the place information and do scoring based on that WHILE (@@fetch_status = 0) BEGIN -- Insert a row into the streak table for this UPDATE MemberStreak SET dnRankForHowLongStreak = @Y WHERE MemberID = @lv_nMemberID IF (@@error <> 0) BEGIN ROLLBACK TRAN GOTO theend END -- Get next row FETCH c_GetAllStreaksCurr INTO @lv_nMemberID, @lv_nHowLong SET @X = @X + 1 -- If the members are not tied, the @Y (place) to the current team position (@X) IF (@lv_nHowLong <> @lv_nPreviousHowLong) SELECT @Y = @X, @lv_nPreviousHowLong = @lv_nHowLong END -- (@@fetch_status = 0)COMMIT TRAN |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-05-11 : 11:58:52
|
| ummm... it might be a little easier if you explain to us what data you have and what you need to return, with a small sample if possible.Also, check the articles here at SQLTeam I have written one on a set-based way to calculate streaks in your data ... though I have no idea if that applies to your situation or not.- Jeff |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-11 : 12:09:17
|
| The query gets members and streaks so the data could be like this:memberID howLong2123125 22234214 13242523 15324234 152342525 2332432 22The cursor loops through and ranks based on the streakallowing for ties. ExamplememberID Rank howLong2123125 2 22234214 6 13242523 4 15324234 4 152342525 1 2332432 2 22 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-11 : 12:33:24
|
| I forgot to mention...The main thing I need to accomplish is ranking the members by longest streak. Ties result in having the same rank, so I need to compare the member to the previous member to see if the streak is the same. |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-11 : 12:42:36
|
| I forgot to mention...The main thing I need to accomplish is ranking the members by longest streak. Ties result in having the same rank, so I need to compare the member to the previous member to see if the streak is the same. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-05-11 : 12:53:40
|
This might need cleaning up... but it should work.  Create Table #myTable (memberID bigint, howLong int)Insert Into #myTableSelect 2123125, 22 UnionSelect 234214, 1 UnionSelect 3242523, 15 UnionSelect 324234, 15 UnionSelect 2342525, 23 UnionSelect 32432, 22Order By 2 descgoAlter Table #myTable Add rank intgoSelect * From #myTablegoUpdate #myTableSet rank = (Select count(*) From #myTable B Where howLong >= A.howLong)-- and memberId<>A.memberId)From #myTable A goUpdate ASet rank = A.rank-1From #myTable A Inner Join #myTable BOn A.rank = B.rankand A.memberId <> B.memberIdgoSelect * From #myTablegoDrop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-11 : 13:26:24
|
| Thanks Seventhnightworked great! |
 |
|
|
|
|
|
|
|