我必须使用某些条件对SQL Server记录进行洗牌

时间:2018-11-05 06:59:14

标签: sql-server

我有一个按行号排序的StudentId的表,我想用某些逻辑计算的值更新NewDummyNumber列。

这是我的原始表格:

|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840   |15005    |1     |NULL      |
|282841   |15006    |2     |NULL      |
|282877   |15040    |3     |NULL      |
|282878   |15041    |4     |NULL      |
|282879   |15042    |5     |NULL      |
|282880   |15043    |6     |NULL      |
|282881   |15044    |7     |NULL      |
|282882   |15045    |8     |NULL      |
|282837   |15002    |9     |NULL      |
|282838   |15003    |10    |NULL      |
---------------------------------------

NewDummyColumn的逻辑是:

如果SkipNumber = 2,则记录将像第1条记录一样被随机播放,其虚拟编号为1001,则现在跳过两条记录,第4条记录的新虚拟编号为1002,依此类推,直到10,然后此循环将继续。结果应该像

-第一个循环

|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840   |15005    |1     |1001      |  
|282841   |15006    |2     |NULL      |
|282877   |15040    |3     |NULL      |
|282878   |15041    |4     |1002      |
|282879   |15042    |5     |NULL      |
|282880   |15043    |6     |NULL      |
|282881   |15044    |7     |1003      |
|282882   |15045    |8     |NULL      |
|282837   |15002    |9     |NULL      |
|282838   |15003    |10    |1004      |
---------------------------------------

第二个周期:

|StudentId|RegNumber|RowNum|NewDummyNo|
+---------+---------+------+----------+
|282840   |15005    |1     |1001      |  
|282841   |15006    |2     |NULL      |
|282877   |15040    |3     |1005      |
|282878   |15041    |4     |1002      |
|282879   |15042    |5     |NULL      |
|282880   |15043    |6     |1006      |
|282881   |15044    |7     |1003      |
|282882   |15045    |8     |NULL      |
|282837   |15002    |9     |1007      |
|282838   |15003    |10    |1004      |
---------------------------------------

第三个周期

|StudentId|RegNumber|RowNum|NewDummyNo|
---------------------------------------
|282840   |15005    |1     |1001      |  
|282841   |15006    |2     |1008      |
|282877   |15040    |3     |1005      |
|282878   |15041    |4     |1002      |
|282879   |15042    |5     |1009      |
|282880   |15043    |6     |1006      |
|282881   |15044    |7     |1003      |
|282882   |15045    |8     |1010      |
|282837   |15002    |9     |1007      |
|282838   |15003    |10    |1004      |
---------------------------------------

现在,NewDummyNo列不包含任何NULL值,因此这是最终结果。

请建议如何实现-我的#table是:

create table #DUMMYNUMBER 
(
     StudentId int,
     OrderStudentRegNumber int,
     RwNumber int,
     NewDummyNumber int
)

insert into #DUMMYNUMBER(StudentId, OrderStudentRegNumber, RwNumber) 
values 
    (282840, 15005, 1),
    (282841, 15006, 2),
    (282877, 15040, 3),
    (282878, 15041, 4),
    (282879, 15042, 5),
    (282880, 15043, 6),
    (282881, 15044, 7),
    (282882, 15045, 8),
    (282837, 15002, 9),
    (282838, 15003, 10)

1 个答案:

答案 0 :(得分:0)

如果记录数和skipNumber + 1是互质数,则可以执行以下操作:

DECLARE @skip int = 2;
DECLARE @total int = (SELECT COUNT(*) FROM #DUMMYNUMBER);
DECLARE @skip int = @skipNumber+1;

SELECT TOP(@total) StudentId, OrderStudentRegNumber, RwNumber, 1000+ROW_NUMBER() OVER (ORDER BY N) NewDummyNo FROM ( 
    SELECT *, R+A*@total N, CASE WHEN (R+A*@total)%@skip=1 THEN 'X' END X FROM (
        SELECT *, ROW_NUMBER() OVER (ORDER BY RwNumber) R
        FROM #DUMMYNUMBER
    ) Num
    CROSS APPLY (VALUES (0),(1),(2)) T(A) --number of loops, equal to (skip+1), use tally or recursive generator if needed
) T WHERE X IS NOT NULL

如果不是,则会发生无限循环。