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 selection

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2012-09-10 : 09:19:33
Hi,

I have one query which returns me 100 records in result set. is it possible to select only 5 randomly from that resultset.

Thanks
Subha

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-10 : 09:23:04
select top (5) * from
(here comes your select)dt
order by NEWID()


Too old to Rock'n'Roll too young to die.
Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2012-09-10 : 09:28:33
i need random selection, not top count.

by using tamplesample we collect based on percentage, but for exact count random collection?

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-10 : 09:29:42
order by newid() will give random top 5 from your select/result


Too old to Rock'n'Roll too young to die.
Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2012-09-10 : 09:39:02
fine thanks a lot webfred.
Go to Top of Page
   

- Advertisement -