Subject | Re: [ib-support] select Random-records |
---|---|
Author | Svein Erling Tysvær |
Post date | 2001-09-07T14:47:07Z |
With the latest Firebird release you can reduce the danger of records not
existing (I've never used it, so my syntax is most likely wrong):
SELECT FIRST 1 * FROM "blah"
WHERE ID >= (
SELECT cast(
rand() * (SELECT gen_id("G_blah",0) FROM RDB$DATABASE)
AS SMALLINT )
FROM <sometable>
ORDER BY ID
It is not perfect and records following a not used generator value will
have twice the chance of being selected. You could still end up with no
returned record if the latest generator value was discarded - so a MAX(ID)
(with a descending index on ID, of course) is safer than using gen_id(x,0).
To return more than one record, you would have to use several selects - or
use something else than SELECT FIRST.
HTH,
Set
existing (I've never used it, so my syntax is most likely wrong):
SELECT FIRST 1 * FROM "blah"
WHERE ID >= (
SELECT cast(
rand() * (SELECT gen_id("G_blah",0) FROM RDB$DATABASE)
AS SMALLINT )
FROM <sometable>
ORDER BY ID
It is not perfect and records following a not used generator value will
have twice the chance of being selected. You could still end up with no
returned record if the latest generator value was discarded - so a MAX(ID)
(with a descending index on ID, of course) is safer than using gen_id(x,0).
To return more than one record, you would have to use several selects - or
use something else than SELECT FIRST.
HTH,
Set