Re: Inserting Multiple Random Rows - Mailing list pgsql-sql
| From | Justin Graf |
|---|---|
| Subject | Re: Inserting Multiple Random Rows |
| Date | |
| Msg-id | 4BD86B65.3070006@magwerks.com Whole thread Raw |
| In response to | Inserting Multiple Random Rows (Gary Chambers <gwchamb@gmail.com>) |
| Responses |
Re: Inserting Multiple Random Rows
|
| List | pgsql-sql |
That won't work because Order by does not recompute Random() once gets a
number it stops
you need to generate a series of random numbers then select each record
one at a time out of cities table . You will have to write a plsql
function to do this As any join will cause the result to be ordered.
Here is example of pl-sql procedure that inserts records randomly into a
table from a another table. This is an excerpt from the function.
There is more logic that limits the random result set size and rules
not to repeat a given number.
--First create cursor of the source records OPEN _questions SCROLL for (Select * from questions
where quest_expire > now()::date and trim( both '' from quest_type) =
_classexams.exam_quest_type and trim( both '' from quest_level) =
_classexams.exam_level order by quest_id );
--need to limit the number range created by random so not to exceed the
record count created by the Cursor
select count(quest_id) into _rcount from educate.questions where quest_expire > now()::date
and trim( both '' from quest_type) =
_classexams.exam_quest_type and trim( both '' from quest_level) =
_classexams.exam_level ;
Generate a Random list of of numbers
for _randlist IN (Select num from ( select round(random()*1000)::int as num from
generate_series(1,100000)) rand where num <= _rcount and num > 0 ) LOOP
FETCH ABSOLUTE _randlist.num from _questions into _quest ;
Next Insert into Into the destination Table
end loop;
On 4/28/2010 12:22 PM, Gary Chambers wrote:
> All,
>
> I have a table of user addresses that I'm trying to randomly populate
> with data from a cities table. Using the following query:
>
> INSERT INTO useraddrs(userid, addrdesc, city, stprov)
> SELECT u.userid, 'Home', c.cityname, c.stateabbr
> FROM users u, cities c
> WHERE u.userid NOT IN (SELECT userid FROM useraddrs)
> AND cid=(SELECT cid FROM cities ORDER BY RANDOM() LIMIT 1);
>
> I am able to achieve most of what I am trying to accomplish, but once
> the random number is selected, it doesn't change. What am I missing?
> Thank you in advance.
>
> -- Gary Chambers
>
> /* Nothing fancy and nothing Microsoft! */
>
>
All legitimate Magwerks Corporation quotations are sent in a .PDF file attachment with a unique ID number generated by
ourproprietary quotation system. Quotations received via any other form of communication will not be honored.
CONFIDENTIALITY NOTICE: This e-mail, including attachments, may contain legally privileged, confidential or other
informationproprietary to Magwerks Corporation and is intended solely for the use of the individual to whom it
addresses.If the reader of this e-mail is not the intended recipient or authorized agent, the reader is hereby notified
thatany unauthorized viewing, dissemination, distribution or copying of this e-mail is strictly prohibited. If you have
receivedthis e-mail in error, please notify the sender by replying to this message and destroy all occurrences of this
e-mailimmediately.
Thank you.