How to select random records

M

mr.splarf

Does anyone know what SQL I should use in Access to
return a random set of records ?
i.e. everytime the user presses the refresh button on a
web-page, I want the SQL to select x records completely
at random from a given table.
The records returned should be randomly chosen each time
the select is run (ie not reiterate the same random
numbers each time).
I've got a table with about 40,000 records, and I want a
random 10 records, so obviously I don't want to select
everything and then pick 10 at random afterwards; I want
the initial SQL to just pick 10 records at random and
then return them.
Thanks.
 
M

[MVP] S. Clark

SELECT TOP 10 [OrderID] Mod (9*Rnd()+1) AS Expr1, Orders.OrderID
FROM Orders
WHERE ((([OrderID] Mod (9*Rnd()+1))=0))
ORDER BY [OrderID] Mod (9*Rnd()+1), Orders.OrderID DESC;

--
HTH,

Steve Clark, Access MVP
FMS, Inc.
Professional Solutions Group
http://www.FMSInc.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Is your Access database too slow?
Are you ready to upgrade to SQL Server?
Contact us for optimization and/or upsizing!
http://www.FMSInc.com/consulting
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top