Select top n records (randomized)

G

Guest

I use a query like this:
SELECT TOP n *
FROM db
ORDER BY RND(Autonumber_Field)
Is it possible to get the value for "n" from a table where it was introduced
before?
This is necessary because the number n is sometimes, not very often, changed.

Thank you in advance.
 
A

Allen Browne

Try:
SELECT TOP n *, RND(Autonumber_Field)
FROM db
ORDER BY RND(Autonumber_Field)

While I can't guarantee that this will give you the right results, I would
be very surprised if the query optimizer called the function twice for the
same value. It generally assumes that the function will return the same
result if the arguments are the same, and so doesn't call it again if
nothing changed.
 
G

Guest

Thank a lot, Mr Allen, but it seems that Access doesn't understand what is n.
I think this is normally because n is not defined at the moment when the
query runs. I assume that n already exists in another database than the
mentioned "db".
- Mani -
 
A

Allen Browne

You will need to hard code the number you want (e.g. 5) in place of n in the
query statement.
 

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