pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ozer, Pam <pozer@automotive.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Random sort with distinct
Date: Sat, 02 Oct 2010 09:52:50 -0400
Message-ID: <5070.1286027570@sss.pgh.pa.us> (raw)
In-Reply-To: <216FFB77CBFAEE4B8EE4DF0A939FF1D10182C8@mail-001.corp.automotive.com>
References: <216FFB77CBFAEE4B8EE4DF0A939FF1D10182C8@mail-001.corp.automotive.com>

"Ozer, Pam" <pozer@automotive.com> writes:
> Select Distinct VehicleMake, VehicleModel
> From VehicleYearMakeModelTrim
> Order by random()
> Limit 10;

> I don't want to bring back the random number I just want the sort order
> to be random. How can I sort randomly?  This query breaks because
> random() is not in the select.

Well, yeah: the implication of the ORDER BY is that a new random value
is to be computed for each row of VehicleYearMakeModelTrim.  After you
combine rows with DISTINCT it's not clear which of those values should
be used to sort a grouped row.

You need to put the DISTINCT and the ORDER BY in separate query levels,
like this:

select * from
  (Select Distinct VehicleMake, VehicleModel
   From VehicleYearMakeModelTrim) ss
Order by random()
Limit 10;

			regards, tom lane



view thread (5+ messages)  latest in thread

Message-ID: <5070.1286027570@sss.pgh.pa.us>
Permalink:  ../5070.1286027570@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5070.1286027570@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pozer@automotive.com
  Subject: Re: Random sort with distinct
  In-Reply-To: <5070.1286027570@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox