从SQL Server表中选择唯一的随机行,但总是复制 [英] Select unique random rows from SQL Server table but always duplicates
问题描述
我是不是最好使用SQL,但我尽我所能得到完成我的问题。我有一个充满列的表公正(ID(PK,身份),煤焦,SERV,随机)。现在,我想从这个表中选择一个随机行并将其插入到表f_WinGet。到目前为止,我所有的程序走这一步很好,但我总是在第二个表重复。
i am not the best with sql but i try my best to get my Problems done. I have a table "just" which is filled with Columns ( ID (PK, Identity), Char, Serv , Random ) . Now i want to select a random row from this table and insert it into the Table "f_WinGet". So far all my Procedures take this Step fine , but i always get Duplicates in the second table.
第一个表:84行
二表:需要35随机出84
First table : 84 rows Second table: needed 35 random out of 84.
我试过很多其他方法,但我总是得到相同的结果。我随机过程全部都绑定到一个按钮,我一个C#PROGRAMM。所有工作正常,到目前为止,但我一直都在我的表行的一些重复。
I have tried many other ways but i always get the same result. All my Procedure for random are binded to a Button i a C# Programm. All is working fine so far , but i always have some Duplicates of Rows in my Table.
INSERT INTO f_TWinGet
SELECT TOP 1 Percent Char, Serv, Random
FROM ( select distinct Char, Serv, Random from dbo.just) as derived1
ORDER BY CHECKSUM(NEWID())
这将是很好,如果有人HASE一个想法如何,我可以解决我的问题。我仍然在努力,但所有我所得到的都是一样的结果。
It would be nice , if anyone hase an Idea how i can fix my Problem. I am still trying , but all what i get are always the same result.
推荐答案
随着小到你可以将表使用这样的:
With a table as small as yours you can use something like:
INSERT INTO f_TWinGet
SELECT TOP 1 j.Char, j.Serv, j.Random
FROM dbo.just j
LEFT JOIN f_TWinGet f
ON f.Char = j.Char
AND j.Serv = f.Serv
AND j.Random = f.Random
WHERE f.Char IS NULL
ORDER BY NEWID()
这样,确保你试图插入值不在决赛桌。
This way making sure that the values you're trying to insert is not on the final table.
这篇关于从SQL Server表中选择唯一的随机行,但总是复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!