Re: [SQL] More fun with random selects
От | Tom Lane |
---|---|
Тема | Re: [SQL] More fun with random selects |
Дата | |
Msg-id | 22060.930752931@sss.pgh.pa.us обсуждение исходный текст |
Ответ на | More fun with random selects (Darren Greer <dgreer@websightsolutions.com>) |
Список | pgsql-sql |
Darren Greer <dgreer@websightsolutions.com> writes: > For example, lets say I want a random 125 rows from an existing table. Is > there an easy way I can do that where I actually am getting a decent sample > from the table? I don't think there's any way to do that with a simple SQL command. It's possible to draw exactly N items from a set fully randomly if you are willing to write a little code. The algorithm looks like this: itemsRemaining = size of set (# rows in table);itemsStillNeeded = N (# rows wanted, 125 in your example);foreach (item inset){ generate random value X that is 'true' with probability itemsStillNeeded / itemsRemaining; if (X) { emit current item as a selected item; decrement itemsStillNeeded; if (itemsStillNeeded == 0) done; } decrement itemsRemaining;} The random choice is typically done with a random number generator that generates output G between say 0 and M; you make X 'true' if G <= M * itemsStillNeeded / itemsRemaining. regards, tom lane
В списке pgsql-sql по дате отправления: