Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to use dbms_random.seed()

mayakiaApr 16 2020 — edited Apr 23 2020

Hello, I'm a beginner of SQL.

Could you help me how to use 'dbms_random.seed()' ?

I want to set seed and select 100 patients' data randomly from source data set.

Source data set is like below. It contains more than 1700000 patients' data. One patient can have multiple records.

From this source data set, I want to select 100 patients randomly and extract their data with date >=2015-01-01.

The 100 patients should have drugcode=1000 with date >=2016-04-01.

PATIENTID
DATE
DRUGCODE
1112015-4-151000
1112014-6-201001
1112016-7-61002
3312014-12-31010
3312019-1-41000
9992018-10-131009
9992018-6-151001
9992018-1-71007
4112017-5-141000
4112016-11-291010
2012014-7-191002
2012018-8-111003
1012014-5-191000
1512019-12-41006
1512016-6-101005
1512020-1-301000
8882014-11-71010
8882018-12-251000

In this example, we have 4 patients(331, 411,151,and 888) who have drug code=1000 after 2016-04-01.

Lets say we want to randomly-select 2 patients among these 4.

If the selected 2 patients were 411 and 888, the desired result is like below.

One of the records of patient 888 should be removed because date is not after 2015-01-01.

PATIENTID
DATE
DRUGCODE
4112017-5-141000
4112016-11-291010
8882018-12-251000

I tried the following SQL in SPL PLUS but it seemed that SEED did not work.

I executed 'call' every time with the same seed number, but got data of different 100 patients.

========================================================

call dbms_random.seed(11111);

SELECT source.*

FROM source

WHERE

  source.date >=2015-01-01  AND

  source.patientid IN

  (

    SELECT b.patientid

    FROM

    (

      SELECT rownum, a.*, dbms_random.value() RAND

      FROM

      (

        SELECT DISTINCT source.patientid

        FROM source

        WHERE

          source.drugcode = 1000 AND

          source.date>=2016-04-01

        ORDER BY source.patientid

      ) a

      ORDER BY RAND

    ) b

    WHERE ROWNUM<=100

  )

====================================================

Strangely, when I tried the following SQL , SEED does work.

I got the same 100 PATIENTID who have drug code=1000 after 2016-04-01.

====================================================

call dbms_random.seed(11111);

    SELECT b.patientid

    FROM

    (

      SELECT rownum, a.*, dbms_random.value() RAND

      FROM

      (

        SELECT DISTINCT source.patientid

        FROM source

        WHERE

          source.drugcode = 1000 AND

          source.date>=2016-04-01

        ORDER BY source.patientid

      ) a

      ORDER BY RAND

    ) b

    WHERE ROWNUM<=100

========================================================

Is there any problem to put this SQL into WHERE IN () ??

I really appreciate your help.

Thank you!

Comments
Post Details
Added on Apr 16 2020
12 comments
3,558 views