使用随机行和重复项防止重复

时间:2014-09-15 18:31:36

标签: sql random random-sample distinct-values newid

我有一个数据集,我需要随机选择符合特定条件的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 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

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