Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id C674813364DE for ; Sat, 2 Oct 2010 10:52:57 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 61971-01 for ; Sat, 2 Oct 2010 13:52:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id A71161335B36 for ; Sat, 2 Oct 2010 10:52:50 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o92DqoKZ005071; Sat, 2 Oct 2010 09:52:50 -0400 (EDT) To: "Ozer, Pam" cc: pgsql-sql@postgresql.org Subject: Re: Random sort with distinct In-reply-to: <216FFB77CBFAEE4B8EE4DF0A939FF1D10182C8@mail-001.corp.automotive.com> References: <216FFB77CBFAEE4B8EE4DF0A939FF1D10182C8@mail-001.corp.automotive.com> Comments: In-reply-to "Ozer, Pam" message dated "Fri, 01 Oct 2010 13:51:38 -0700" Date: Sat, 02 Oct 2010 09:52:50 -0400 Message-ID: <5070.1286027570@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/5 X-Sequence-Number: 34656 "Ozer, Pam" 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