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 records

Author  Topic 

vmagana
Starting Member

1 Post

Posted - 2011-09-28 : 13:15:01
I have a table with millions of records spanning over 1041 counties nationwide. I am trying to get 10 random records from each county.

I have a column named HIST_COUNTY_NAME, what select statement can I use to get all the columns with 10 random records from each county??

Big Vic

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-28 : 13:17:32
[code]SELECT columns....
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY HIST_COUNTY_NAME ORDER BY NEWID()) AS Rn,*
FROM table
)t
WHERE rn<=10
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vmvadivel
Yak Posting Veteran

69 Posts

Posted - 2011-09-28 : 21:41:47
--Using NEWID() as long as it is a *smaller* table!
SELECT TOP 10 * FROM Person.Address ORDER BY NEWID()

If its a large table then we shouldn't be using NEWID as it would have bad performance. Try to make use of BINARY_CHECKSUM instead. Refer - http://msdn.microsoft.com/en-us/library/cc441928.aspx

Best Regards
Vadivel

http://vadivel.blogspot.com
Go to Top of Page

paultech
Yak Posting Veteran

79 Posts

Posted - 2011-09-29 : 10:50:24
kindly find the following link

http://blog.sqlauthority.com/2010/08/01/sql-server-introduction-to-binary_checksum-and-working-example/

Go to Top of Page
   

- Advertisement -