我需要一种方法来获取KeyID的结果集,并将其尽可能平均地划分,并根据KeyID对每个部门的记录进行更新。换句话说,有
SELECT KeyID
FROM TableA
WHERE (some criteria exists)
我想通过3个相等的KeyID部分以3种不同的方式更新TableA。
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)
或类似的东西。感谢您的所有回复。
最佳答案
With TiledItems As
(
Select KeyId
, NTILE(3) OVER( ORDER BY ... ) As NTileNum
From TableA
Where ...
)
Update TableA
Set FieldA = Case TI.NTileNum
When 1 Then Value1
When 2 Then Value2
When 3 Then Value3
End
From TableA As A
Join TiledItems As TI
On TI.KeyId = A.KeyId
关于sql - SQL Server/T-SQL:如何更新结果集的相等百分比?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2804694/