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>
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
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
I am coming back to this problem. I used the NEWID() method and it seemd to work fine BUT it is giving double outputs. Several affiliates are mentioned several times in the same list.
<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, NEWID()
</cfquery>
Then I added a
<!--- [bof] query of query because the select distinct cannot be used in the former query because of the newid() --->
<cfquery name="lastupdates" dbtype="query">
SELECT DISTINCT handelaaruuid
, projectID
, naam
, websiteurl
, gemeente
, animatie
, type_adverteerder
, categoryid
, subcatid
,datum
,datum2
FROM LastUpdates
ORDER BY datum desc
</cfquery>
The DISTINCT should make there are no more doubles but nope.
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
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) .
An exemple of a dump. My queries:
<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, handelaaruuid DESC, NEWID()
</cfquery>
<!--- [bof] query of query because the select distinct cannot be used in the former query because of the newid() --->
<cfquery name="lastupdatesall" dbtype="query">
SELECT DISTINCT handelaaruuid
, projectID
, naam
, websiteurl
, gemeente
, animatie
, type_adverteerder
, categoryid
, subcatid
,datum
,datum2
FROM LastUpdates
ORDER BY datum desc
</cfquery>
| query | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| ANIMATIE | CATEGORYID | DATUM | DATUM2 | GEMEENTE | HANDELAARUUID | NAAM | PROJECTID | SUBCATID | TYPE_ADVERTEERDER | WEBSITEURL | |
| 1 | [empty string] | 2152 | 2011-07-08 12:47:59.0 | 08 Jul 2011 | Nordhorn | 1291CCB1-D8CB-C4AB-8EB56A1BC3DEED06 | Nostalgie Palast | 1 | 3523 | 6 | www.nostalgiepalast.de |
| 2 | [empty string] | 108 | 2011-07-08 12:38:17.0 | 08 Jul 2011 | Eindhoven | E160412C-0EEB-4C37-3D80F9E5D21D65C9 | De Jachtkamer | 1 | 0 | 6 | www.dejachtkamer.nl |
| 3 | [empty string] | 67 | 2011-07-08 12:25:37.0 | 08 Jul 2011 | Brussel | 79B4E35B-FFB0-4FB1-B48FC20EECF9F6D8 | Mesman-Kinet Brussels Antiques | 1 | 0 | 6 | www.brusselsantiques.com/ |
| 4 | [empty string] | 24 | 2011-07-08 12:11:51.0 | 08 Jul 2011 | Eindhoven | 7F5FB322-DBB0-D733-1E8EC03D33D39A62 | Berry Swinkels Antiek | 1 | 0 | 6 | www.berryswinkelsantiek.nl |
| 5 | [empty string] | 23 | 2011-07-08 11:57:11.0 | 08 Jul 2011 | Utrecht | 0580B54B-90D1-D7D9-C62B687AC6048E2E | Van Leest Medical & Scientific Instruments | 1 | 0 | 6 | www.vanleestantiques.com |
| 6 | [empty string] | 32 | 2011-07-08 08:19:23.0 | 08 Jul 2011 | Tielt | 9AE70E01-D85B-CC6F-7266926B5F71A01D | Antiques Wholesale Dealers | 1 | 0 | 6 | www.antiqueswholesaledealers.com |
| 7 | 5 dealers | 1 | 2011-07-08 08:13:11.0 | 08 Jul 2011 | Pepinster | 5DE689B9-CED2-7684-A72B022BD8B1EFCC | Johny Masset Antiquités | 1 | 0 | 6 | www.masset-antiquites.com |
| 8 | [empty string] | 389 | 2011-07-08 07:43:01.0 | 08 Jul 2011 | Marcq - Enghien | 9FFADF48-8C6A-4599-A0ACCDEB1BB132BA | Eurotroc Export - FREE packing ! | 1 | 282 | 1 | www.EuroTrocBelgium.com |
| 9 | [empty string] | 1140 | 2011-07-08 07:32:45.0 | 08 Jul 2011 | Gembloux & Hilversum | 9415ACEC-FB2D-0066-FF9BA564C8895182 | Status Aparte | 1 | 0 | 6 | www.statusaparte.be |
| 10 | [empty string] | 67 | 2011-07-08 07:21:05.0 | 08 Jul 2011 | Zegge | E10770E3-B181-DE88-B579980DD5A4432A | Gieling Louis Oudheden | 1 | 0 | 6 | www.louisgieling.nl |
| 11 | [empty string] | 38 | 2011-07-08 07:20:10.0 | 08 Jul 2011 | Eindhoven | 96C4F4D0-883E-42C9-9FA5CDEFB380AF46 | Religious Church Art | 1 | 0 | 6 | www.religieusekunst.nl |
| 12 | [empty string] | 3643 | 2011-07-08 06:47:52.0 | 08 Jul 2011 | Zwevezele - Wingene | 22B5416D-DAE6-1B94-676B33B8D5F7A4BB | MD Antiques | 1 | 0 | 6 | www.mdantiques.be/ |
| 13 | [empty string] | 15 | 2011-07-08 06:43:33.0 | 08 Jul 2011 | Heist-op-den-Berg | 71B68829-7293-4916-A519347895A23A86 | Medussa | 1 | 0 | 1 | www.medussa.be |
| 14 | [empty string] | 32 | 2011-07-08 06:35:54.0 | 08 Jul 2011 | Lommel | 80DD50D4-695F-4DB0-849887FB94B259FF | Nent Antiques | 1 | 0 | 6 | www.nentnv.be |
| 15 | [empty string] | 34 | 2011-07-08 06:35:54.0 | 08 Jul 2011 | Lommel | 80DD50D4-695F-4DB0-849887FB94B259FF | Nent Antiques | 1 | 32 | 6 | www.nentnv.be |
| 16 | [empty string] | 35 | 2011-07-08 06:35:54.0 | 08 Jul 2011 | Lommel | 80DD50D4-695F-4DB0-849887FB94B259FF | Nent Antiques | 1 | 32 | 6 | www.nentnv.be |
| 17 | [empty string] | 38 | 2011-07-08 06:35:54.0 | 08 Jul 2011 | Lommel | 80DD50D4-695F-4DB0-849887FB94B259FF | Nent Antiques | 1 | 32 | 6 | www.nentnv.be |
The idea is that the field "NAAM" only shows up one time in the output. NAAM is dutch for NAME.
Any ideas??
Bianca
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 ![]()
I need these data to output them. It is about a list of most recent updates
of different affiliates. It shows most recent but at the same time these
need to be random to give each affiliate equall chance. Otherwise it is
always the same ones who do just some updates at the end of the day in order
to end up high in the list. The list I output is like
NAAM (category)
URL (date) (animation)
I am using MS SQL 2008, I found on Google that NEWID () should be the way to
do this random selection
Bianca
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?
Please have a look at http://beta.antiek.com/ you have a recent updates
section, that is the output.
Bianca
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()
This is a really interesing approach, i didn't know of this. However there
is still one problem. The table with last updates is old, each time updates
get added but they never removed updates. So there are records from 2007,
2008 etc. I changed they order by NEWID() in ORDER BY datum2 DESC, NEWID()
But in the list that gets generated we also see some records that shouldn't
be there because they are old.
Re: Is it possible to randomly output query
Schäfer<http://www.schaferantiek.com/>
Kasterlee (28-05-08)
Re: Is it possible to randomly output query Kalpers
Antiques<http://www.kalpers.be/>
Liege (28-02-08)
It is as if the ORDER BY doesn't work, maybe because of the dateformat in
the db?? Example 12/21/2009 03:14:15 AM
Do you have any idea??
Bianca
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.
In fact i just use the order by Datum2 because I want to prevent older records to show up. Today my boss changed his mind, he wants the order by date desc and within those with the same date the random order. Well, that is just what I was proposing him with your help:-)
I have learnt a lot these last weeks from the forum, like they say in french "chapeau bas" for those who take the time to help people out.
Bianca
... 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
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
North America
Europe, Middle East and Africa
Asia Pacific