Results 1 to 6 of 6

Thread: Random order in Access and SQL Server

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Random order in Access and SQL Server

    I need to retrive a recordset in a random order, the table is going to be used for a banner display. So I want to select a random record, or at least order the recordset in a random order.

    Also it can only be done with sql code, cant do it with asp or similar, and the same code should work in Access and SQL server.....

    I have been trying with something like this:

    SELECT FieldID, FieldNAME
    FROM Table
    ORDER BY Rnd(FieldID)

    FieldID would be a number/integer.

    Would this acctually give me a random order, have tested some, but arent 100% sure that its "random".

    Also, could one throw in some "weight" into the random?, like give one row in the table some more "possible hits"?
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Random order in Access and SQL Server

    SQL Server doesn't do random I'm afraid (search for posts by szlamany that contain "random" to see the best option).

    If Rnd in Access is the same as Rnd in VB (as most functions are), your usage of Rnd is actually incorrect, it should be: rnd() * maximum_value
    (ie: the parameter is omitted).

    If the numbers are actually random, your method would be ok for Access databases.

    If you want this to work for multiple databases, the best option (i know it isn't one you want), is to do the randomisation at the front end.

  3. #3
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: Random order in Access and SQL Server

    try this
    SELECT * FROM table
    ORDER BY NEWID()

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    Re: Random order in Access and SQL Server

    NewID only works in SQL Server, not access.

    My usage of rnd() in access seams to be correct.

    My concern is that it might not give me a correct random.
    Visual Basic
    C, C++
    Java
    Access
    SQL Server

    MCP, MCSD

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Random order in Access and SQL Server

    RND() in a MS SQL SELECT statement is only evaluated once in the QUERY - so you get the same value in each and every row. This is particularly poor of SQL - but that's the way it works.

    Look at this thread:

    http://www.vbforums.com/showthread.p...&highlight=rnd

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: Random order in Access and SQL Server

    To think about this in another way- for your banner display it doesn't matter whether the order is random, it is just important that there does not appear to be any pattern. What that means is that there can actually be a pattern, so long as its cycle is long enough that no user notices it.
    You could therefore take another apporach. Supposing your table of items for banner display has 100 items. Create a table (tbl_ViewOrder or something) with 10,000 records (or 1,000,000 if you want to eliminate any chance of a user spotting a pattern), and containing the id of the item to be viewed and an autonumber field. You can create it using VB, or even manually, so that items can be strictly random or, as you said you wanted, the randomness can be weighted.
    You can then select 100 records apparently at random using
    SELECT TOP 100 tbl_vieworder.ID, YourTable.* FROM tbl.vieworder INNER JOIN yourtable ON YourTable.ItemID=tbl_ViewOrder.ItemID WHERE tbl_ViewOrder.ID > rnd() * 999900
    Last edited by anguswalker; Nov 6th, 2005 at 12:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width