Tuesday, May 5, 2009

SQL Trick

Found a trick today for SQL from this site. I needed to get a random sample from a dataset, where each record has a unique ID record.

According to the site, the query to get one random record by its ID is:

"SELECT TOP 1 someColumn
FROM someTable
ORDER BY NEWID() "

If you change Top 1 to Top 50, you can get 50 random records according to their ID (someColumn in this case). Of course you can use any number you wish.

No comments: