Subject | select Random-records |
---|---|
Author | raptor |
Post date | 2001-09-07T11:30:13Z |
hi,
I want to say give me Y-count records (from just one table, not
a join or something), but want these records to be choosed on Random
principe ... keep in mind that the ID's are not guaranteed to be
sequental... i.e there can be holes in sequence ( u know generators :") ).
I thought but it seemed this is not possible to be done in just one Select
query... may be some SP !!
Possibly something like this (pseudo code) :
:count - contains how many records are needed
SELECT gen_id("G_blah", 0) FROM RDB$Database
INTO :lastElem
WHILE :count-- DO
BEGIN
SELECT fieldX FROM Table WHERE ID = CAST(Rand(:lastElem) AS INTEGER);
INTO :result
IF :result THEN SUSPEND
END
any better ideas !! I still want to make this in one query :")
=====
iVAN
raptor@...
=====
PS. My tries until now with single select :
SELECT * FROM "blah"
WHERE ID = (
SELECT cast(
rand() * (SELECT gen_id("G_blah",0) FROM RDB$DATABASE)
AS SMALLINT )
FROM RDB$DATABASE
sequntaly-increacing ID's are not guaranteed, so it not work and is useable
for only one record I need 4-5... so other idea is to have subselect that
returns all ID's and I pick from it some way ..!!!
Other way::..
It may be possible if in some way I can get x-th record from subselect...or
something like this :
SELECT ....
WHERE sub-select(by-ID) IN ( rand1,rand2,...randX )
=====
iVAN
raptor@...
=====
I want to say give me Y-count records (from just one table, not
a join or something), but want these records to be choosed on Random
principe ... keep in mind that the ID's are not guaranteed to be
sequental... i.e there can be holes in sequence ( u know generators :") ).
I thought but it seemed this is not possible to be done in just one Select
query... may be some SP !!
Possibly something like this (pseudo code) :
:count - contains how many records are needed
SELECT gen_id("G_blah", 0) FROM RDB$Database
INTO :lastElem
WHILE :count-- DO
BEGIN
SELECT fieldX FROM Table WHERE ID = CAST(Rand(:lastElem) AS INTEGER);
INTO :result
IF :result THEN SUSPEND
END
any better ideas !! I still want to make this in one query :")
=====
iVAN
raptor@...
=====
PS. My tries until now with single select :
SELECT * FROM "blah"
WHERE ID = (
SELECT cast(
rand() * (SELECT gen_id("G_blah",0) FROM RDB$DATABASE)
AS SMALLINT )
FROM RDB$DATABASE
sequntaly-increacing ID's are not guaranteed, so it not work and is useable
for only one record I need 4-5... so other idea is to have subselect that
returns all ID's and I pick from it some way ..!!!
Other way::..
It may be possible if in some way I can get x-th record from subselect...or
something like this :
SELECT ....
WHERE sub-select(by-ID) IN ( rand1,rand2,...randX )
=====
iVAN
raptor@...
=====