Select n random rows from SQL Server table

前端 未结 16 873
陌清茗
陌清茗 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

    Just order the table by a random number and obtain the first 5,000 rows using TOP.

    SELECT TOP 5000 * FROM [Table] ORDER BY newid();
    

    UPDATE

    Just tried it and a newid() call is sufficent - no need for all the casts and all the math.

提交回复
热议问题