Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Random Numbers

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2011-06-03 : 17:00:41
I have query ive written where i pull out column a which is all distinct and a grouping column, that is either a 1,2,3

I need to pull 10% of the results at random from the group but make sure there is an even distribution of the three options in the second column.

Any ideas?

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-03 : 17:57:35
If you are on SQL 2008, you can use TABLESAMPLE (http://msdn.microsoft.com/en-us/library/ms189108.aspx ). However, the rows returned may not be truly random.

If you do want truly random, select top 10 percent, but using an order by clause that involves newid function and a column in your table, so that the newid function will be invoked per row. For example:
select top 10 percent *
from yourTable
order by RAND(CHECKSUM(NEWID())%1000000000 + AColumnINTheTable)


In both the above cases, the distribution will be roughly proportional to the distribution of the grouping column in your table.
Go to Top of Page
   

- Advertisement -