SQL Server / T-SQL : How to update equal percentages of a resultset?

Posted by Kent Comeaux on Stack Overflow See other posts from Stack Overflow or by Kent Comeaux
Published on 2010-05-10T17:02:06Z Indexed on 2010/05/10 17:34 UTC
Read the original article Hit count: 231

Filed under:
|
|

I need a way to take a resultset of KeyIDs and divide it up as equally as possible and update records differently for each division based on the KeyIDs. In other words, there is

SELECT KeyID
FROM   TableA
WHERE (some criteria exists)

I want to update TableA 3 different ways by 3 equal portions of KeyIDs.

UPDATE TableA
SET    FieldA = Value1
WHERE  KeyID IN (the first 1/3 of the SELECT resultset above)

UPDATE TableA
SET    FieldA = Value2
WHERE  KeyID IN (the second 1/3 of the SELECT resultset above)

UPDATE TableA
SET    FieldA = Value3
WHERE  KeyID IN (the third 1/3 of the SELECT resultset above)

or something to that effect. Thanks for any and all of your responses.

© Stack Overflow or respective owner

Related posts about sql

Related posts about t-sql