I have 2 tables which are linked by userid. There is a column in the first table that I need to update based on the way the table would be sorted using criteria from another table. For example:
In this example, the result set is sorted by the start column from @a, and then by the blah column from @b. The result from the select statement returns the data in the correct order. However, I need to populate the tiebreaker column in @a based on the way the result set would be sorted from the blah column in @b, and grouped by start in @a.
For example, the result set from the above query would look like this if the tiebreaker column was already populated:
Since ccc, aaa, and bbb all shared the same start date, they were assigned incrementing tiebreakers determined by the sort order from the blah column. The same with fff, ddd, and eee.
The data in the blah column will be somewhat unpredictable - so I couldn't think of an equation to perform on it to generate the tiebreaker. The only other way I could think to solve this problem was with a loop or a cursor. Is there a simpler and faster set-based query that will update the values correctly?
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
Code:
[COLOR=blue]set[/color] [COLOR=#FF00FF]nocount[/color] [COLOR=blue]on[/color]
[COLOR=blue]declare[/color] @a [COLOR=blue]table[/color] (userid [COLOR=blue]char[/color](3), [COLOR=#FF00FF]start[/color] [COLOR=blue]smalldatetime[/color], tiebreaker [COLOR=blue]tinyint[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'aaa'[/color], [COLOR=red]'2007-01-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'bbb'[/color], [COLOR=red]'2007-01-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'ccc'[/color], [COLOR=red]'2007-01-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'ddd'[/color], [COLOR=red]'2007-02-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'eee'[/color], [COLOR=red]'2007-02-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'fff'[/color], [COLOR=red]'2007-02-01'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @a [COLOR=blue]values[/color] ([COLOR=red]'ggg'[/color], [COLOR=red]'2007-03-01'[/color], 0)
[COLOR=blue]declare[/color] @b [COLOR=blue]table[/color] (userid [COLOR=blue]char[/color](3), blah [COLOR=blue]smallint[/color])
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'aaa'[/color], 5)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'bbb'[/color], 10)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'ccc'[/color], -1)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'ddd'[/color], 69)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'eee'[/color], 1134)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'fff'[/color], 0)
[COLOR=blue]insert[/color] [COLOR=blue]into[/color] @b [COLOR=blue]values[/color] ([COLOR=red]'ggg'[/color], 1)
[COLOR=blue]set[/color] [COLOR=#FF00FF]nocount[/color] [COLOR=blue]off[/color]
[COLOR=blue]select[/color] a.userid, a.start, a.tiebreaker, b.blah
[COLOR=blue]from[/color] @a a
[COLOR=blue]inner[/color] [COLOR=blue]join[/color] @b b
[COLOR=blue]on[/color] a.userid = b.userid
[COLOR=blue]order[/color] [COLOR=blue]by[/color] a.start, b.blah
In this example, the result set is sorted by the start column from @a, and then by the blah column from @b. The result from the select statement returns the data in the correct order. However, I need to populate the tiebreaker column in @a based on the way the result set would be sorted from the blah column in @b, and grouped by start in @a.
For example, the result set from the above query would look like this if the tiebreaker column was already populated:
Code:
userid start tiebreaker blah
------ --------------------------------- ---------- ------
ccc 2007-01-01 00:00:00 0 -1
aaa 2007-01-01 00:00:00 [!]1[/!] 5
bbb 2007-01-01 00:00:00 [!]2[/!] 10
fff 2007-02-01 00:00:00 0 0
ddd 2007-02-01 00:00:00 [!]1[/!] 69
eee 2007-02-01 00:00:00 [!]2[/!] 1134
ggg 2007-03-01 00:00:00 0 1
(7 row(s) affected)
Since ccc, aaa, and bbb all shared the same start date, they were assigned incrementing tiebreakers determined by the sort order from the blah column. The same with fff, ddd, and eee.
The data in the blah column will be somewhat unpredictable - so I couldn't think of an equation to perform on it to generate the tiebreaker. The only other way I could think to solve this problem was with a loop or a cursor. Is there a simpler and faster set-based query that will update the values correctly?
-kaht
Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson