| От | Martijn van Oosterhout |
|---|---|
| Тема | Re: Obtaining random rows from a result set |
| Дата | |
| Msg-id | 20070831135416.GA23673@svana.org обсуждение исходный текст |
| Ответ на | Obtaining random rows from a result set (Alban Hertroys <alban@magproductions.nl>) |
| Ответы |
Re: Obtaining random rows from a result set
|
| Список | pgsql-general |
On Fri, Aug 31, 2007 at 02:42:18PM +0200, Alban Hertroys wrote:
> Examples:
> * random(maxrows) would return random rows from the resultset.
> * median() would return the rows in the middle of the result set (this
> would require ordering to be meaningful).
It would be possible to write an aggregate that returns a single random
value from a set. The algorithm is something like:
n = 1
v = null
for each row
if random() < 1/n:
v = value of row
n = n + 1
return v
It does require a seqscan though. If you're asking for 5 random rows
you probably mean 5 random but distinct rows, which is different to
just running the above set 5 times in parallel.
I don't know if there's a similar method for median...
Have a nice day,
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.
В списке pgsql-general по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера