pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Random sort with distinct
5+ messages / 4 participants
[nested] [flat]

* Random sort with distinct
@ 2010-10-01 20:51  Ozer, Pam <pozer@automotive.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Ozer, Pam @ 2010-10-01 20:51 UTC (permalink / raw)
  To: pgsql-sql

I have the following query

 

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.

 

Thanks

 

Pam

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Random sort with distinct
@ 2010-10-02 13:40  Dmitriy Igrishin <dmitigr@gmail.com>
  parent: Ozer, Pam <pozer@automotive.com>
  2 siblings, 1 reply; 5+ messages in thread

From: Dmitriy Igrishin @ 2010-10-02 13:40 UTC (permalink / raw)
  To: Ozer, Pam <pozer@automotive.com>; +Cc: pgsql-sql

Hey Ozer,

How about dynamic queries?

2010/10/2 Ozer, Pam <pozer@automotive.com>

>  I have the following query
>
>
>
> 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.
>
>
>
> Thanks
>
>
>
> Pam
>
>
>
>


-- 
// Dmitriy.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Random sort with distinct
@ 2010-10-02 13:42  Lee Hachadoorian <lee.hachadoorian@gmail.com>
  parent: Ozer, Pam <pozer@automotive.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Lee Hachadoorian @ 2010-10-02 13:42 UTC (permalink / raw)
  To: pgsql-sql

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
This runs fine on my 8.4 install. What version are you using and what
error message are you getting?<br>
<br>
--Lee<br>
<br>
On 10/01/2010 04:51 PM, Ozer, Pam wrote:
<blockquote
 cite="mid:216FFB77CBFAEE4B8EE4DF0A939FF1D10182C8@mail-001.corp.automotive.com"
 type="cite">
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1">
  <meta name="Generator" content="Microsoft Word 12 (filtered medium)">
  <style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
	{mso-style-priority:99;
	mso-style-link:"Balloon Text Char";
	margin:0in;
	margin-bottom:.0001pt;
	font-size:8.0pt;
	font-family:"Tahoma","sans-serif";}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
span.BalloonTextChar
	{mso-style-name:"Balloon Text Char";
	mso-style-priority:99;
	mso-style-link:"Balloon Text";
	font-family:"Tahoma","sans-serif";}
.MsoChpDefault
	{mso-style-type:export-only;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
-->
  </style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
  <div class="WordSection1">
  <p class="MsoNormal">I have the following query<o:p></o:p></p>
  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
  <p class="MsoNormal">Select Distinct VehicleMake, VehicleModel<o:p></o:p></p>
  <p class="MsoNormal">From VehicleYearMakeModelTrim<o:p></o:p></p>
  <p class="MsoNormal">Order by random()<o:p></o:p></p>
  <p class="MsoNormal">Limit 10;<o:p></o:p></p>
  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
  <p class="MsoNormal">I don&#8217;t want to bring back the random number I
just
want the sort order to be random. How can I sort randomly?&nbsp; This query
breaks
because random() is not in the select.<o:p></o:p></p>
  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
  <p class="MsoNormal">Thanks<o:p></o:p></p>
  <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
  <p class="MsoNormal">Pam<o:p></o:p></p>
  <p class="MsoNormal"><span
 style="font-size: 12pt; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;;"><br>
  <br>
  </span><o:p></o:p></p>
  </div>
</blockquote>
<br>
<pre class="moz-signature" cols="72">-- 
Lee Hachadoorian
PhD Student, Geography
Program in Earth &amp; Environmental Sciences
CUNY Graduate Center
</pre>
</body>
</html>



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Random sort with distinct
@ 2010-10-02 13:52  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Ozer, Pam <pozer@automotive.com>
  2 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2010-10-02 13:52 UTC (permalink / raw)
  To: Ozer, Pam <pozer@automotive.com>; +Cc: pgsql-sql

"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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Random sort with distinct
@ 2010-10-04 15:40  Ozer, Pam <pozer@automotive.com>
  parent: Dmitriy Igrishin <dmitigr@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Ozer, Pam @ 2010-10-04 15:40 UTC (permalink / raw)
  To: Dmitriy Igrishin <dmitigr@gmail.com>; +Cc: pgsql-sql

What about dynamic queries?

 

From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Dmitriy Igrishin
Sent: Saturday, October 02, 2010 6:40 AM
To: Ozer, Pam
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Random sort with distinct

 

Hey Ozer,

How about dynamic queries?

2010/10/2 Ozer, Pam <pozer@automotive.com>

I have the following query

 

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.

 

Thanks

 

Pam

 




-- 
// Dmitriy.





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2010-10-04 15:40 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-10-01 20:51 Random sort with distinct Ozer, Pam <pozer@automotive.com>
2010-10-02 13:40 ` Dmitriy Igrishin <dmitigr@gmail.com>
2010-10-04 15:40   ` Ozer, Pam <pozer@automotive.com>
2010-10-02 13:42 ` Lee Hachadoorian <lee.hachadoorian@gmail.com>
2010-10-02 13:52 ` Tom Lane <tgl@sss.pgh.pa.us>

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