Quantcast
Channel: Select n random rows from SQL Server table - Stack Overflow
Viewing all articles
Browse latest Browse all 20

Answer by klyd for Select n random rows from SQL Server table

$
0
0

Didn't quite see this variation in the answers yet. I had an additional constraint where I needed, given an initial seed, to select the same set of rows each time.

For MS SQL:

Minimum example:

select top 10 percent *from table_nameorder by rand(checksum(*))

Normalized execution time: 1.00

NewId() example:

select top 10 percent *from table_nameorder by newid()

Normalized execution time: 1.02

NewId() is insignificantly slower than rand(checksum(*)), so you may not want to use it against large record sets.

Selection with Initial Seed:

declare @seed intset @seed = Year(getdate()) * month(getdate()) /* any other initial seed here */select top 10 percent *from table_nameorder by rand(checksum(*) % @seed) /* any other math function here */

If you need to select the same set given a seed, this seems to work.


Viewing all articles
Browse latest Browse all 20

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>