防止随机行和重复的重复
Posted
技术标签:
【中文标题】防止随机行和重复的重复【英文标题】:Preventing duplicates with random rows and duplicates 【发布时间】:2014-09-15 18:31:35 【问题描述】:我有一个数据集,我需要随机选择满足特定条件的 x 行、满足其他条件的 x 行等,并将结果全部输出到一个表中。我一直在使用以下内容:
SELECT * FROM (SELECT TOP 1000 * FROM dbo.[Client List]
WHERE Source = 'HC' AND Service = 'Service1' AND Provider = 'ProviderName1'
ORDER BY NEWID()) Table1
UNION ALL
SELECT * FROM (SELECT TOP 500 * FROM dbo.[Client List]
WHERE Source = 'HC' AND Service = 'Service2' AND Provider = 'ProviderName2'
ORDER BY NEWID()) Table2
UNION ALL
SELECT * FROM (SELECT TOP 2200 * FROM dbo.[Client List]
WHERE Source = 'BA' AND Service = 'Service3' AND Provider = 'ProviderName3'
ORDER BY NEWID()) Table3
这可行,但有一个问题:它允许重复的客户端标识符 (dbo.[Client List].[ClientID] 被选中。我所有的结果都必须是唯一的客户端。
换句话说,它必须进行第一次随机选择,然后进行第二次随机选择,而不能选择在第一次选择中选择的任何 ClientID,依此类推。 (是的,我意识到这在技术上不是“随机的”。)有没有办法可以在每个后续 SELECT 的 WHERE 语句中添加某种代码,或者我是否需要重新考虑代码的整个结构?谢谢!
【问题讨论】:
【参考方案1】:这样的事情怎么样?
with Service1 as
(
SELECT TOP 1000 *
FROM dbo.[Client List]
WHERE Source = 'HC'
AND Service = 'Service1'
AND Provider = 'ProviderName1'
ORDER BY NEWID()
)
, Service2 as
(
SELECT TOP 500 *
FROM dbo.[Client List]
WHERE Source = 'HC'
AND Service = 'Service2'
AND Provider = 'ProviderName2'
AND ClientID not in (select ClientID from Service1)
ORDER BY NEWID()
)
, Service3 as
(
SELECT TOP 2200 *
FROM dbo.[Client List]
WHERE Source = 'BA'
AND Service = 'Service3'
AND Provider = 'ProviderName3'
AND ClientID not in (select ClientID from Service1)
AND ClientID not in (select ClientID from Service2)
ORDER BY NEWID()
)
select *
from Service1
union all
select *
from Service2
union all
select *
from Service3
【讨论】:
以上是关于防止随机行和重复的重复的主要内容,如果未能解决你的问题,请参考以下文章