From: Justin Graf <justin@magwerks.com>
To: Gary Chambers <gwchamb@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Inserting Multiple Random Rows
Date: Wed, 28 Apr 2010 13:07:49 -0400
Message-ID: <4BD86B65.3070006@magwerks.com> (raw)
In-Reply-To: <q2x302670f21004280922ic25b4f91k968effc6926a6243@mail.gmail.com>
References: <q2x302670f21004280922ic25b4f91k968effc6926a6243@mail.gmail.com>
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 our proprietary 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 information proprietary 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 that any unauthorized viewing, dissemination, distribution or copying of this e-mail is strictly prohibited. If you have received this e-mail in error, please notify the sender by replying to this message and destroy all occurrences of this e-mail immediately.
Thank you.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: justin@magwerks.com, gwchamb@gmail.com
Subject: Re: Inserting Multiple Random Rows
In-Reply-To: <4BD86B65.3070006@magwerks.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox