How do I generate random number for each row in a TSQL Select?

前端 未结 19 978
逝去的感伤
逝去的感伤 2020-11-22 07:03

I need a different random number for each row in my table. The following seemingly obvious code uses the same random value for each row.

SELECT table_name,          


        
相关标签:
19条回答
  • 2020-11-22 07:37

    When called multiple times in a single batch, rand() returns the same number.

    I'd suggest using convert(varbinary,newid()) as the seed argument:

    SELECT table_name, 1.0 + floor(14 * RAND(convert(varbinary, newid()))) magic_number 
    FROM information_schema.tables
    

    newid() is guaranteed to return a different value each time it's called, even within the same batch, so using it as a seed will prompt rand() to give a different value each time.

    Edited to get a random whole number from 1 to 14.

    0 讨论(0)
  • 2020-11-22 07:39

    try using a seed value in the RAND(seedInt). RAND() will only execute once per statement that is why you see the same number each time.

    0 讨论(0)
  • 2020-11-22 07:39

    If you don't need it to be an integer, but any random unique identifier, you can use newid()

    SELECT table_name, newid() magic_number 
    FROM information_schema.tables
    
    0 讨论(0)
  • 2020-11-22 07:39

    The problem I sometimes have with the selected "Answer" is that the distribution isn't always even. If you need a very even distribution of random 1 - 14 among lots of rows, you can do something like this (my database has 511 tables, so this works. If you have less rows than you do random number span, this does not work well):

    SELECT table_name, ntile(14) over(order by newId()) randomNumber 
    FROM information_schema.tables
    

    This kind of does the opposite of normal random solutions in the sense that it keeps the numbers sequenced and randomizes the other column.

    Remember, I have 511 tables in my database (which is pertinent only b/c we're selecting from the information_schema). If I take the previous query and put it into a temp table #X, and then run this query on the resulting data:

    select randomNumber, count(*) ct from #X
    group by randomNumber
    

    I get this result, showing me that my random number is VERY evenly distributed among the many rows:

    enter image description here

    0 讨论(0)
  • 2020-11-22 07:39
    Update my_table set my_field = CEILING((RAND(CAST(NEWID() AS varbinary)) * 10))
    

    Number between 1 and 10.

    0 讨论(0)
  • 2020-11-22 07:40
        DROP VIEW IF EXISTS vwGetNewNumber;
        GO
        Create View vwGetNewNumber
        as
        Select CAST(RAND(CHECKSUM(NEWID())) * 62 as INT) + 1 as NextID,
        'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'as alpha_num;
    
        ---------------CTDE_GENERATE_PUBLIC_KEY -----------------
        DROP FUNCTION IF EXISTS CTDE_GENERATE_PUBLIC_KEY;  
        GO
        create function CTDE_GENERATE_PUBLIC_KEY()
        RETURNS NVARCHAR(32)
        AS 
        BEGIN
            DECLARE @private_key NVARCHAR(32);
            set @private_key = dbo.CTDE_GENERATE_32_BIT_KEY();
            return @private_key;
        END;
        go
    
    ---------------CTDE_GENERATE_32_BIT_KEY -----------------
    DROP FUNCTION IF EXISTS CTDE_GENERATE_32_BIT_KEY;  
    GO
    CREATE function CTDE_GENERATE_32_BIT_KEY()
    RETURNS NVARCHAR(32)
    AS 
    BEGIN
        DECLARE @public_key NVARCHAR(32);
        DECLARE @alpha_num NVARCHAR(62);
        DECLARE @start_index INT = 0;
        DECLARE @i INT = 0;
        select top 1 @alpha_num = alpha_num from vwGetNewNumber;
            WHILE @i < 32
            BEGIN
              select top 1 @start_index = NextID from vwGetNewNumber;
              set @public_key = concat (substring(@alpha_num,@start_index,1),@public_key);
              set @i = @i + 1;
            END;
        return @public_key;
    END;
        select dbo.CTDE_GENERATE_PUBLIC_KEY() public_key;
    
    0 讨论(0)
提交回复
热议问题