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.
| 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)tWHERE rn<=10[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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.aspxBest RegardsVadivelhttp://vadivel.blogspot.com |
 |
|
|
paultech
Yak Posting Veteran
79 Posts |
|
|
|
|
|