Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 01C799FC2D0 for ; Fri, 23 Feb 2007 15:42:36 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 69972-05 for ; Fri, 23 Feb 2007 15:42:29 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.226]) by postgresql.org (Postfix) with ESMTP id 441F79FC2DD for ; Fri, 23 Feb 2007 15:42:30 -0400 (AST) Received: by wr-out-0506.google.com with SMTP id i7so722519wra for ; Fri, 23 Feb 2007 11:42:29 -0800 (PST) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=WAcdXvaxSPbpmy3IITlv+DHDJjj9V6eiQD2Cih3i7dOEH776SawcUMjEq+Dnku0nmTzP6l+84Wxlcl5hTid4Jvx3UjeltjAX8ePX+ayuMkEKDLN610LYhUXzNsGRtrs5+m5S42FRRX4CQj4/S3l3hcCdzv2U+Q9usMfg3QvUxaU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=tSA+NY3GQygtCHG3IXjK1dM2guhX1jYud2+S4wOCKGawNuwn0+O/QVvZ+g4NsuK/1XHOcNriHTB1r/gciQcYj3R1Jw66M83ZcfW/68V8nnMcIMNbfzyF2WKF0y9wQ4er9kT1MfYf+JZKeKtwY8FFzCAqioj3Up5pAoUe3lF3Npw= Received: by 10.114.192.1 with SMTP id p1mr1187940waf.1172259744254; Fri, 23 Feb 2007 11:42:24 -0800 (PST) Received: by 10.114.174.18 with HTTP; Fri, 23 Feb 2007 11:42:23 -0800 (PST) Message-ID: Date: Sat, 24 Feb 2007 01:12:23 +0530 From: "Rajesh Kumar Mallah" To: Sumeet Subject: Re: selecting random row values in postgres Cc: pgsql-sql@postgresql.org In-Reply-To: <7539aebb0702231129s48399f26r8881c6eff81b0708@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_48412_7913715.1172259743796" References: <7539aebb0702231129s48399f26r8881c6eff81b0708@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200702/242 X-Sequence-Number: 27576 ------=_Part_48412_7913715.1172259743796 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 2/24/07, Sumeet wrote: > Hi all, > > I'm trying to write a query to select random values from a set of 'GROUP > BY' > ....see the scenario below to understand the problem here (the actual > problem cannot be discussed here so i'm taking an example scenario) > > Assume there is a table > > id | name | year_of_birth > > query: I want to select for each year_of_birth a random name. Dear Sumeet postgresql DISTINCT ON may be of help , but its not standard sql. regds mallah. tradein_clients=> SELECT * from temp.test; +----+------+-----+ | id | name | yob | +----+------+-----+ | 1 | A | 2 | | 2 | B | 2 | | 3 | C | 2 | | 4 | D | 1 | | 5 | E | 1 | | 6 | F | 1 | +----+------+-----+ (6 rows) tradein_clients=> SELECT distinct on (yob) id,name,yob from temp.test order by yob,random(); +----+------+-----+ | id | name | yob | +----+------+-----+ | 5 | E | 1 | | 1 | A | 2 | +----+------+-----+ (2 rows) tradein_clients=> SELECT distinct on (yob) id,name,yob from temp.test order by yob,random(); +----+------+-----+ | id | name | yob | +----+------+-----+ | 4 | D | 1 | | 1 | A | 2 | +----+------+-----+ (2 rows) > > --> so i do a group by year_of_birth, now i have a set of names, is there > any function to select just one name from these set of names. > The current approach i'm using to solve this problem is > > 1) getting these names in a single string using a custom function > 'group_concat' > 2) Convert the single string into an array > 3) use postgresql random function to generate a random number > 4) us the random number to select a element from the array previously > created. > > The solution is there but it's kinda hack, is there any other better way of > solving this problem. > > > Thanks, > Sumeet ------=_Part_48412_7913715.1172259743796 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

On 2/24/07, Sumeet <asumeet@gmail.com> wrote:
> Hi all,
>
> I'm trying to write  a query to select random values from a set of 'GROUP
> BY'
> ....see the scenario below to understand the problem here (the actual
> problem cannot be discussed here so i'm taking an example scenario)
>
> Assume there is a table
>
> id | name | year_of_birth
>
> query: I want  to select for each year_of_birth a random name.

Dear Sumeet

postgresql DISTINCT ON may be of help , but its not standard sql.

regds
mallah.

tradein_clients=> SELECT * from temp.test;
+----+------+-----+
| id | name | yob |
+----+------+-----+
|  1 | A    |   2 |
|  2 | B    |   2 |
|  3 | C    |   2 |
|  4 | D    |   1 |
|  5 | E    |   1 |
|  6 | F    |   1 |
+----+------+-----+
(6 rows)

tradein_clients=> SELECT distinct on (yob) id,name,yob  from temp.test order by yob,random();
+----+------+-----+
| id | name | yob |
+----+------+-----+
|  5 | E    |   1 |
|  1 | A    |   2 |
+----+------+-----+
(2 rows)

tradein_clients=> SELECT distinct on (yob) id,name,yob  from temp.test order by yob,random();
+----+------+-----+
| id | name | yob |
+----+------+-----+
|  4 | D    |   1 |
|  1 | A    |   2 |
+----+------+-----+
(2 rows)




>
> --> so i do a group by year_of_birth, now i have a set of names, is there
> any function to select just one name from these set of names.
> The current approach i'm using to solve this problem is
>
> 1) getting these names in a single string using a custom function
> 'group_concat'
> 2) Convert the single string into an array
> 3) use postgresql random function to generate  a random number
> 4) us the random number to select a element from the array previously
> created.
>
> The solution is there but it's kinda hack, is there any other better way of
> solving this problem.
>
>
> Thanks,
> Sumeet
------=_Part_48412_7913715.1172259743796--