Select n random rows from SQL Server table

前端 未结 16 1172
陌清茗
陌清茗 2020-11-22 10:54

I\'ve got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I\'ve thought of a complicated way, creating a temp table wi

16条回答
  •  忘掉有多难
    2020-11-22 11:43

    This is a combination of the initial seed idea and a checksum, which looks to me to give properly random results without the cost of NEWID():

    SELECT TOP [number] 
    FROM table_name
    ORDER BY RAND(CHECKSUM(*) * RAND())
    

提交回复
热议问题