Re: [GENERAL] Obtaining random rows from a result set

2007-09-04 Thread Alban Hertroys
Alban Hertroys wrote: > To follow up on my own post, I came up with a workable solution based on > scrolling cursors. The SP approach didn't work out for me, I didn't > manage to declare a cursor in PL/pgSQL that could be positioned > absolutely (maybe that's due to us still using PG 8.1.something?

Re: [GENERAL] Obtaining random rows from a result set

2007-09-04 Thread Alban Hertroys
To follow up on my own post, I came up with a workable solution based on scrolling cursors. The SP approach didn't work out for me, I didn't manage to declare a cursor in PL/pgSQL that could be positioned absolutely (maybe that's due to us still using PG 8.1.something?). A solution to that would b

Re: [GENERAL] Obtaining random rows from a result set

2007-09-01 Thread Alban Hertroys
On Sep 1, 2007, at 14:44, Martijn van Oosterhout wrote: On Sat, Sep 01, 2007 at 02:24:25PM +0200, Alban Hertroys wrote: Oh, now I see... The first time guarantees that v has a value (as random() < 1/1), and after that there is a decreasing chance that a new row gets re-assigned to v. That means

Re: [GENERAL] Obtaining random rows from a result set

2007-09-01 Thread Martijn van Oosterhout
On Sat, Sep 01, 2007 at 02:24:25PM +0200, Alban Hertroys wrote: > Oh, now I see... The first time guarantees that v has a value (as > random() < 1/1), and after that there is a decreasing chance that a > new row gets re-assigned to v. That means the last row has a chance > of 1/n, which would

Re: [GENERAL] Obtaining random rows from a result set

2007-09-01 Thread Alban Hertroys
On Sep 1, 2007, at 12:44, Alban Hertroys wrote: 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 Doesn't this always return

Re: [GENERAL] Obtaining random rows from a result set

2007-09-01 Thread Alban Hertroys
On Aug 31, 2007, at 15:54, Martijn van Oosterhout wrote: 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 b

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Erik Jones
On Aug 31, 2007, at 8:34 AM, Kaloyan Iliev wrote: Alban Hertroys wrote: Hello, I've recently been busy improving a query that yields a fixed number of random records matching certain conditions. I have tried all the usual approaches, and although they do work, they're all limited in some

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Kaloyan Iliev
Hi, Why not generate a random number in your application and then: SELECT * FROM table_x WHERE condition = true OFFSET generated_random_number LIMIT xx Kaloyan Iliev Alban Hertroys wrote: Hello, I've recently been busy improving a query that yields a fixed number of random records matching c

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Martijn van Oosterhout
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 aggrega

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Csaba Nagy
> Dear Santa, > > I'd like my database to have functionality analogue to how LIMIT works, > but for other - non-sequential - algorithms. There was some discussion before to possibly reuse the algorithm ANALYZE is using for sampling some given percentage of the table data and provide this for some

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Albe Laurenz
Alban Hertroys wrote: > I've recently been busy improving a query that yields a fixed > number of random records matching certain conditions. > Dear Santa, > > I'd like my database to have functionality analogue to how > LIMIT works, > but for other - non-sequential - algorithms. > > I was thin

Re: [GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Josh Tolley
On 8/31/07, Alban Hertroys <[EMAIL PROTECTED]> wrote: > Hello, > > I've recently been busy improving a query that yields a fixed number of > random records matching certain conditions. I have tried all the usual > approaches, and although they do work, they're all limited in some way > and don't tr

[GENERAL] Obtaining random rows from a result set

2007-08-31 Thread Alban Hertroys
Hello, I've recently been busy improving a query that yields a fixed number of random records matching certain conditions. I have tried all the usual approaches, and although they do work, they're all limited in some way and don't translate really well to what you "want". They're kludges, IMHO. T