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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting a random record from top 100 records

Author  Topic 

sqlcc
Starting Member

1 Post

Posted - 2007-01-07 : 21:39:08
Hope someone can help me with the following issue:

I would like to randomly pick up a row from a table. However the row that is randomly selected should be from the most recently (say, 50 or 100) posted entry in the table.

I was able to randomly pick a record (through rand()) or sort the top entries (through limit). However I am not able to combine both the above requirements.

Thanks in advance for your suggestions!
--

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-01-07 : 22:05:12
here's one way:

select top 1 theFirst100.* from
(select top 100 * from YourTable order by YourColumn asc) theFirst100
order by newid()

However since you say you are using the limit keyword, you are probably not using SQL Server. I am guessing you are using mysql.

this forum is for SQL Server questions only. try http://www.dbforums.com for other RDBMS.


www.elsasoft.org
Go to Top of Page
   

- Advertisement -