Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 6A4AB9FB851 for ; Fri, 23 Feb 2007 15:29:18 -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 68252-05 for ; Fri, 23 Feb 2007 15:29:13 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.246]) by postgresql.org (Postfix) with ESMTP id CD1A29FB769 for ; Fri, 23 Feb 2007 15:29:14 -0400 (AST) Received: by an-out-0708.google.com with SMTP id c24so446273ana for ; Fri, 23 Feb 2007 11:29:13 -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:mime-version:content-type; b=f94JPeJTzZ77HKEjKl1KqXqSwiic1cjsqD0bwTqrp9kF/IoI+gXNOC4vBjHBzPFiDk0V410C8627pI86mejj4mpP9WTCu8icx33a4j83GgpWaUr3cQ9vwVfW2wry/yeopO86OU/AjGroJv4MWG5NCDQJl/LBuxdLIAWe1s8fEgw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:mime-version:content-type; b=DFVP4S/Hjp2e56S16GM1tg1vDBOFxLXLp0ORCMGWQ4i/Vs98R0Lgn4z2dosKpp3ChLNTGfopSo3TUOR+O9BpDqdsWXEDGiM8uVpiFH+D2e53TS1adAAd7VSLgIRJ37EzI735+L9T6lfqyv8JJfn/RtKKYiVyF7TaJoTcgxYaS04= Received: by 10.100.32.1 with SMTP id f1mr1814417anf.1172258953937; Fri, 23 Feb 2007 11:29:13 -0800 (PST) Received: by 10.100.11.6 with HTTP; Fri, 23 Feb 2007 11:29:13 -0800 (PST) Message-ID: <7539aebb0702231129s48399f26r8881c6eff81b0708@mail.gmail.com> Date: Fri, 23 Feb 2007 14:29:13 -0500 From: Sumeet To: pgsql-sql@postgresql.org Subject: selecting random row values in postgres MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_5357_29700786.1172258953911" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.611 tagged_above=0 required=5 tests=BAYES_20, HTML_10_20, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200702/238 X-Sequence-Number: 27572 ------=_Part_5357_29700786.1172258953911 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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. --> 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_5357_29700786.1172258953911 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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.

--> 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_5357_29700786.1172258953911--