Select n random rows from SQL Server table

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

    select top 10 percent * from [yourtable] order by newid()
    

    In response to the "pure trash" comment concerning large tables: you could do it like this to improve performance.

    select  * from [yourtable] where [yourPk] in 
    (select top 10 percent [yourPk] from [yourtable] order by newid())
    

    The cost of this will be the key scan of values plus the join cost, which on a large table with a small percentage selection should be reasonable.

提交回复
热议问题