好的,這是我在此期間提出的另一種解決方案。我決定用戶已經以隨機順序在系統中,所以也許我應該嘗試一種解碼器環式的方法。因此,此代碼會生成一個移位值,然後移動用戶ID。 select語句用公式「繞回」。這不是完全隨機的,它可以使用一些波蘭語,但這是我的。用戶可能不會察覺到隨機性的缺乏。謝謝您的幫助!
DECLARE @people TABLE
(
id INT,
name VARCHAR(50)
)
INSERT INTO @people VALUES (1,'Matthew')
INSERT INTO @people VALUES (2,'Mark')
INSERT INTO @people VALUES (3,'Luke')
INSERT INTO @people VALUES (4,'John')
INSERT INTO @people VALUES (5,'Doug')
INSERT INTO @people VALUES (6,'Jamie')
INSERT INTO @people VALUES (7,'John')
INSERT INTO @people VALUES (8,'Cameron')
INSERT INTO @people VALUES (9,'Emily')
INSERT INTO @people VALUES (10,'Tyler')
INSERT INTO @people VALUES (11,'Todd')
INSERT INTO @people VALUES (12,'Kathryn')
DECLARE @Random INT;
DECLARE @Upper INT;
DECLARE @Lower INT;
DECLARE @MaxId INT;
SET @Lower = 1 -- The lowest record ID
SELECT @Upper = (MAX(Id)/2) FROM @people -- select random int that is somewhere between 1 and half of max ID for shift
SELECT @MaxId = MAX(Id) FROM @people -- select the largest ID in the table
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0) -- calculate a random number to shift IDs off by
SELECT id, ((id + @Random) % @MaxId) + 1, @Random
from @people
定義沒有重複。你想分配(更新)AssignedUserId列? – Paparazzi
可能的重複[如何從表中隨機選擇唯一行對]?(http://stackoverflow.com/questions/4823208/how-do-i-select-unique-pairs-of-rows-from -a-table-at-random) – Pondlife
對於沒有蠢話,我的意思是每個配對應該是唯一的,沒有人應該被分配給自己。 –