Skip navigation
Currently Being Moderated

Is it possible to randomly output query

Jul 2, 2011 5:51 AM

It is a website with about 400 affiliates, on one page people can see recent updates. Certain affiliates modify on purpose often small things on their website in order to end up high on this list. therefore we find always the same couple of affiliates on top of the list. Is it possible to randomly output the query that gets the most recent updates to give everybody equal chances?

I realise that in the query there is a ORDER BY clause that orders by most recent date. What I need is the 40 most recent updates and output them randomly.

 

<cfquery name="lastupdates" datasource="#request.dbc#">
        SELECT
            handelaaruuid
            , projectID, naam
            , websiteurl
            , gemeente
            , animatie
            , type_adverteerder
            , categoryid
            , subcatid,
             datum_voorlaatste_aanpassing AS datum
             , CONVERT(nvarchar, datum_voorlaatste_aanpassing, 106) AS datum2
        FROM      LastUpdates
        WHERE     (projectID = 1)
        ORDER BY datum desc
    </cfquery>

 
Replies
  • Currently Being Moderated
    Jul 2, 2011 8:24 AM   in reply to bianca_homedev

    I just googled "sql order by random":

    http://lmgtfy.com/?q=sql+order+by+random

     

    There seems to be a bunch of stuff there that answers your question...

     

    PS: if you're asking DB-specific questions, it's important that you tell us what DB system you're running, as the answer will almost always be different from system to system.  there's some good tips on how to post here: http://www.catb.org/~esr/faqs/smart-questions.html

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 2, 2011 8:32 AM   in reply to bianca_homedev

    Fair enough.

     

    As a rule of thumb, I think it's best to do the data manipulation on the DB, and leave CF to do the presentation of the data.  I guess in this case it's arguable whether this is data manipulation or data presentation.  However if the DB can fulfil the data-manipulation requirement... I'll do it on the DB.

     

    Equally, sometimes it's not until one steps back and talks to someone else about an issue that a different approach presents itself.

     

    --
    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 2, 2011 12:08 PM   in reply to bianca_homedev

    You could have included a link to it!  :-p

     

    I'll go have a look around...

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 7, 2011 12:41 PM   in reply to bianca_homedev

    Try adding handelaaruuid to the ORDER BY clause, then use the group="" attibute on your <cfoutput>

     

    <cfoutput query="lastupdates" group="handelaaruuid">

     

     

    There's also a way to do it in the query:

     

    http://www.coldfusionmuse.com/index.cfm/2006/10/27/no.duplicates

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 7, 2011 1:35 PM   in reply to bianca_homedev

    The DISTINCT should make there are no more doubles but nope.

     

    DISTINCT operates on all of the included columns and returns unique combinations of all of those values. Not just the affiliate ID.

     

    How are you defining "doubles"? Seeing a few sample rows would help (including all 10+ columns) .

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 8, 2011 12:50 PM   in reply to bianca_homedev

    The idea is that the field "NAAM" only shows up one time in

    the output.

     

    Okay, but what about all the other data? I assume you need those columns because you are including them in the SELECT. So out of the four records for Nent Antiques", which one do you want to keep and why? For example, do you want the record with the latest date, latest record id, etcetera ...

     

    Also, which database are you using MS SQL 2005?

     

    NAAM is dutch for NAME.

    Learn something new every day

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 8, 2011 3:15 PM   in reply to bianca_homedev

    The list I output is like

    NAAM (category)

    URL (date) (animation)

     

    Okay .. but the SELECT includes more than just those 5 columns. Are you actually using all 11 columns in your output? The answer will determine what adjustments you need to make.

     

    I found on Google that NEWID () should be the way to do this random selection

     

    Yes.  But first we need to identify what the results should be.  Then we can randomize them. Using the data in your dump, can you give an example of what the desired output would look like?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 10, 2011 7:45 AM   in reply to bianca_homedev

    From what I can see, you are only using four columns: naam, url, date, animation.  If you eliminate the unused columns, DISTINCT will return the expected results. Use TOP if you want to limit the results returned.  To order the results randomly, just write it as a derived table. Something like

     

    SELECT  d.NAAM, d.ANIMATIE, d.WEBSITEURL, d.datum2    
    FROM    (
            SELECT  DISTINCT TOP 40 NAAM, ANIMATIE, WEBSITEURL, CONVERT(nvarchar, datum_voorlaatste_aanpassing, 106) AS datum2        
            FROM    LastUpdates
            WHERE   projectID = 1
            ORDER BY datum_voorlaatste_aanpassing
            )  d
    ORDER BY NewID()
    
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2011 6:21 AM   in reply to bianca_homedev

    The date format in the db only matters if you are storing it as text instead of datetime.  If you don't want old records, put something in the where clause of your query to exclude them. 

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2011 9:40 AM   in reply to bianca_homedev
    CONVERT(nvarchar, datum_voorlaatste_aanpassing, 106)

     

    You do not want to ORDER BY datum2 because that value is a nvarchar/string. Strings can sort very differently than date/time values. That is probably why the results are different than you expect.  Since you seem to be using the convert for grouping (not just formatting), try converting "datum2 " back into a date/time object first. Then in your output, use DateFormat or LSDateFormat to display the date however you wish.

     

    ie

          CONVERT(datetime, CONVERT(nvarchar, datum_voorlaatste_aanpassing, 102), 102)  AS Dateum2

     

    So there are records from 2007,2008 etc.

     

    Then as mentioned, you probably want to add filtering to exclude the older records.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2011 12:19 PM   in reply to bianca_homedev

    ... order  by date desc and within those

    with the same date the random order.

    that is just what I was proposing

     

    Great! That order makes more sense to me too

     

    add filtering to exclude the older records.

     

    BTW: You probably should add some sort of date filter, that only considers updates within the last week or last month (depending on how frequently they typically occur). Otherwise, the SELECT DISTINCT may gradually become slower over time because it must process every record in the table.

     

    Cheers

    Leigh

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 12, 2011 6:17 AM   in reply to bianca_homedev

    Since you are using Microsoft SQL Server database, there is a relatively simple solution using a query of a subquery.

     

    The query below will return a random sort of the 40 most current records, using the TOP statement.

     

    A word of caution, when using the TOP statement in a sql query, make sure there are at least the same number of records as for the TOP number. Say if you SELECT TOP 10 records, there must be at least ten records that qualify, if you use SELECT TOP 10 and there is actually only 9 records, the TOP statement will make the database query extremely slow.

     

    Here is a sample query that should give you what you need.

     

     

    SELECT *

    FROM

    ( SELECT TOP 40 *

    FROM TABLE

    ORDER BY DATUM DESC ) a

    ORDER BY NEWID()

     

     

    Michael G. Workman

    michael.g.workman@gmail.com

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 12, 2011 6:29 AM   in reply to Michael G. Workman

    Here is a sample query that should give you what you need.

     

    In their case they need something different than just the last 40 records. Though they did end up using a derived table.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points