473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sort Order - random?

Is there a way to return a random sort order from a query?
Nov 12 '05 #1
5 11309
Greg Brady wrote:
Is there a way to return a random sort order from a query?


Create a function that returns a random number between 1 and the maximum
number of rows your query is likely to return. You can base this
dynamically on the tables you are querying if you wish. Have this
function create a column in the query. Sort by this column.

Nov 12 '05 #2
In Access SQL it is possible to call certain VB Functions directly. In this
case the Rnd function can be called in a query and fed a seed value from the
data being sorted. Here is an example to randomly sort some invoice
numbers:

SELECT xTstSource.InvoiceNo, Rnd([InvoiceNo]) AS RndOrder
FROM xTstSource
ORDER BY Rnd([InvoiceNo]);

xTstSource is just a test table I created for the purpose of this post.

I have used the method that John puts forward and also used this one. This
one is simpler to implement and now is the one I use almost exclusively. I
stumbled across this solution in this forum and so must admit I am standing
on someone else's shoulders here. Thanks to all who answer here.

--
Jeffrey R. Bailey
"Greg Brady" <vz******@verizon.net> wrote in message
news:U0*******************@nwrddc01.gnilink.net...
Is there a way to return a random sort order from a query?

Nov 12 '05 #3
Jeffrey R. Bailey wrote:
[cleaner solution snipped]


Noticed something interesting when I tried this out - I included the
random field in my query results. The results were indeed in random
order, but they did not look like they were sorted by the random order
field either.

Not that this really matters, but could it be being called twice
somehow? Once when it generates the value for the field, and once when
it does the sort?

Nov 12 '05 #4
Yes, that is exactly what is happening, and obviously it only needs to be
called once. My bad, I pasted the example to quickly. Let's try again:

SELECT xTstSource.InvoiceNo
FROM xTstSource
ORDER BY Rnd([InvoiceNo]);
--
Jeffrey R. Bailey
"John Baker" <ba*****@ix.netcom.com> wrote in message
news:U_******************@fe2.columbus.rr.com...
Jeffrey R. Bailey wrote:
[cleaner solution snipped]


Noticed something interesting when I tried this out - I included the
random field in my query results. The results were indeed in random
order, but they did not look like they were sorted by the random order
field either.

Not that this really matters, but could it be being called twice
somehow? Once when it generates the value for the field, and once when
it does the sort?

Nov 12 '05 #5
earlp
1
Jeffrey, wonder if you might have a solution to this situation.
I use your example, using Rnd(IDNbr) where IDNbr is the autonumber
in my Access table. Using this SQL code on my local machine, and I
select the first record, which is always different. But accessing this
.asp file on my host's server, it always brings back the same record.
Earl


[quote=Jeffrey R. Bailey]Yes, that is exactly what is happening, and obviously it only needs to be
called once. My bad, I pasted the example to quickly. Let's try again:

SELECT xTstSource.InvoiceNo
FROM xTstSource
ORDER BY Rnd([InvoiceNo]);
Apr 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: One's Too Many | last post by:
Ran into a strange problem today: 8.1.7 on AIX 4.3.3 Database and applications had been working fine for two years and all of a sudden a couple of regularly-run queries are now no longer...
2
by: Alpay Eno | last post by:
Hello all... I'm using asp to get records from an access database, very similar to the way datagrid would work. The title of each column in my table is a link that alternates the sort order between...
4
by: Jon Hunt | last post by:
Please forgive a newbie question. In MSSQL I can execute a stored procedure (sp_helpsort) that returns the sort order (listing characters) of a database. Is there a DB2 equivalent to this query?...
4
by: Greg Brady | last post by:
Is there a way to return a random sort order from a query?
11
by: James P. | last post by:
Hello, I have a report with the Priority field is used as sort order and grouping. The problem is the data in this Priority field if sorted in ascending order is: High, Low, and Medium. How...
3
by: Bob Dankert | last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For example: I have the array: 1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b and sort it by the second column, and I...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
2
by: adrian.chandler | last post by:
Hi all, I have been using letter and symbol codes such as GNU< GNU\ GNU} GNUˆ in an Access table. I was surprised to see that when the table was sorted on this field, the order is: GNUˆ...
2
by: Chris | last post by:
Dear All, I have a subform in datasheet view whose record source is a table (not a query) A user can right click and customise sort order. Now when the form is closed VBA code saves for subform...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.