Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

selecting random row values in postgres



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

Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group