根据百分比随机分割表格数据

时间:2018-03-28 09:53:06

标签: sql sql-server

我有一个名为Employees的表,有100条记录。列为IDName

100只是一个样本号,它可以有任意数量的记录,如15或115。 抽样应根据下面给出的百分比进行。

现在我想添加新列GroupGroup值为Group1Group2Group3

如何将行分配给组,以便每行都在一个随机选择的组中,但结果分布遵循以下百分比。

65% Employees Should go to Group1
20% Employee Should go to Group2
15% Employee Should go to Group3

3 个答案:

答案 0 :(得分:2)

答案类似于Michal的形式,他的答案也是正确的,但是NTILE可以用作替代,因为它会将数据集拆分为100个相等的块。 ROW_Number不适用于行数大于100的数据集的情况。

select id,a.name,
       case when rn <= 65 then 'group 1'
            else case when rn <= 85 then 'group 2' else 'group 3' end
       end
from
(
    --newid() will generate random order of records
    select ID, name , NTILE(100) OVER (ORDER BY NEWID()) [rn] from dbo.Employees
) [a]

答案 1 :(得分:0)

以下查询包含内部查询,该查询将引入由newid()生成的基于随机顺序的行号。然后,它足以为外部查询group 1分配1到65之间的行(这是100%的65%),group 2用于66到85之间的行数(20%)和group 3其余的(15%)。

select id,name,
       case when rn <= 65 then 'group 1'
            else case when rn <= 85 then 'group 2' else 'group 3' end
       end
from
(
    --newid() will generate random order of records
    select ID, name, ROW_NUMBER() over (order by newid()) [rn] from Employees
) [a]

答案 2 :(得分:0)

您可以尝试以下查询

DECLARE @Employee TABLE (ID INT,Name VARCHAR(100))

DECLARE @numRows int,@i int
SET @numRows = 100
SET @i=1

WHILE @i<@numRows
BEGIN
    INSERT @Employee(ID,Name) SELECT @i,'Test Name' +Convert(VARCHAR(10),@i)
    SET @i=@i+1
END

SELECT A.ID,A.Name,(CASE WHEN A.RowNumber BETWEEN 1 AND 65 THEN 'Group1'
                        WHEN A.RowNumber BETWEEN 66 AND 85 THEN 'Group2'
                        WHEN A.RowNUmber BETWEEN 86 AND 100 THEN 'Group3' ELSE '' END) 'Group' FROM
(SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowNUmber,ID,Name FROM @Employee) A