selecting random row values in postgres
От | Sumeet |
---|---|
Тема | selecting random row values in postgres |
Дата | |
Msg-id | 7539aebb0702231129s48399f26r8881c6eff81b0708@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: selecting random row values in postgres
|
Список | pgsql-sql |
Hi all,<br /><br />I'm trying to write a query to select random values from a set of 'GROUP BY'<br />....see the scenariobelow to understand the problem here (the actual problem cannot be discussed here so i'm taking an example scenario)<br /><br />Assume there is a table<br /><br />id | name | year_of_birth<br /><br />query: I want to select foreach year_of_birth a random name.<br /><br />--> 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. <br />The current approach i'm using to solve this problemis <br /><br />1) getting these names in a single string using a custom function 'group_concat'<br />2) Convert thesingle string into an array <br />3) use postgresql random function to generate a random number <br />4) us the randomnumber to select a element from the array previously created.<br /><br clear="all" />The solution is there but it'skinda hack, is there any other better way of solving this problem.<br /><br /><br />Thanks, <br />Sumeet
В списке pgsql-sql по дате отправления: