Home » SQL & PL/SQL » SQL & PL/SQL » Pick random rows (11g)
Pick random rows [message #627129] Fri, 07 November 2014 12:50 Go to next message
shree_z
Messages: 75
Registered: February 2008
Member
Hi

I have a procedure to select random rows from a base table into a sys_refcursor.

After I select the random rows, I want to update those rows in the base table, which was chosen for selection. How can this be achieved in the procedure.


My code looks like this
begin

open p_rec for

SELECT (some columns) FROM
   (
   select (some columns..)
  from base table
  where (some conditions)
  order by SYS.DBMS_RANDOM.VALUE
   )
WHERE ROWNUM<100


Now I need to update those rows in base table .( I have an update_date column in the base table)

Thanks in advance
Re: Pick random rows [message #627130 is a reply to message #627129] Fri, 07 November 2014 13:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>After I select the random rows,

posted "code" does NOT "select random rows".

does BASE_TABLE have primary Key?
if so, use PK to determine the rows to be updated.
Re: Pick random rows [message #627132 is a reply to message #627130] Fri, 07 November 2014 13:08 Go to previous messageGo to next message
shree_z
Messages: 75
Registered: February 2008
Member
SELECT (some columns) FROM
   (
   select (some columns..)
  from base table
  where (some conditions)
  order by SYS.DBMS_RANDOM.VALUE
   )
WHERE ROWNUM<100


Doesnt the above code take random rows. Please correct if I went wrong in my understanding.

[Updated on: Fri, 07 November 2014 13:08]

Report message to a moderator

Re: Pick random rows [message #627133 is a reply to message #627132] Fri, 07 November 2014 13:14 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Doesnt the above code take random rows
NO!
The returned rows are STRICTLY determined only by the WHERE clause.
The only thing "random" with SQL above is the order of the rows are presented to the invoker.
if the base table rows are not changed by DML, the posted SQL will always return the same 99 row every time (likely in different order)
Re: Pick random rows [message #627135 is a reply to message #627129] Fri, 07 November 2014 13:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
I want to update those rows in the base table, which was chosen for selection. How can this be achieved in the procedure.


If you have complex computation then use SELECT FOR UPDATE and loop on it to UPDATE.
If not then directly use UPDATE.

Re: Pick random rows [message #627136 is a reply to message #627133] Fri, 07 November 2014 13:27 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
BlackSwan wrote on Fri, 07 November 2014 20:14
>Doesnt the above code take random rows
NO!
The returned rows are STRICTLY determined only by the WHERE clause.
The only thing "random" with SQL above is the order of the rows are presented to the invoker.
if the base table rows are not changed by DML, the posted SQL will always return the same 99 row every time (likely in different order)


The "Random Selection" in that way to be working for me.....

SQL> CREATE TABLE t (a NUMBER(10), b NUMBER(10));
Table created.
SQL>
SQL> INSERT INTO t VALUES (1,1);
1 row created.
SQL> INSERT INTO t VALUES (2,2);
1 row created.
SQL> INSERT INTO t VALUES (3,3);
1 row created.
SQL> INSERT INTO t VALUES (4,4);
1 row created.
SQL> INSERT INTO t VALUES (5,5);
1 row created.
SQL> INSERT INTO t VALUES (6,6);
1 row created.
SQL> INSERT INTO t VALUES (7,7);
1 row created.
SQL> INSERT INTO t VALUES (8,8);
1 row created.
SQL> INSERT INTO t VALUES (9,9);
1 row created.

SQL>
SQL> SELECT * FROM (
  2   SELECT * FROM t WHERE a < 5 ORDER BY SYS.DBMS_RANDOM.Value
  3  ) WHERE ROWNUM < 3;

         A          B
---------- ----------
         1          1
         4          4

SQL>
SQL> SELECT * FROM (
  2   SELECT * FROM t WHERE a < 5 ORDER BY SYS.DBMS_RANDOM.Value
  3  ) WHERE ROWNUM < 3;

         A          B
---------- ----------
         4          4
         2          2
SQL>
SQL> SELECT * FROM (
  2   SELECT * FROM t WHERE a < 5 ORDER BY SYS.DBMS_RANDOM.Value
  3  ) WHERE ROWNUM < 3;

         A          B
---------- ----------
         1          1
         3          3
SQL>

Re: Pick random rows [message #627137 is a reply to message #627135] Fri, 07 November 2014 13:30 Go to previous messageGo to next message
shree_z
Messages: 75
Registered: February 2008
Member
Thanks for the replies BlackSwan and Michel

@BlackSwan Could you please suggest a way to select random rows? Thanks for correcting me.


Thanks
Re: Pick random rows [message #627138 is a reply to message #627129] Fri, 07 November 2014 13:30 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
You could try something like this:
SELECT {Some Columns}
  FROM Base_Table SAMPLE ( 100 )
 WHERE {Some Conditions};

[Updated on: Fri, 07 November 2014 13:31] by Moderator

Report message to a moderator

Re: Pick random rows [message #627139 is a reply to message #627138] Fri, 07 November 2014 13:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

NO, SAMPLE(100) is invalid.

Re: Pick random rows [message #627140 is a reply to message #627137] Fri, 07 November 2014 13:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Could you please suggest a way to select random rows?


See Thomas' post and tell us if this is what you want.

Re: Pick random rows [message #627141 is a reply to message #627140] Fri, 07 November 2014 14:32 Go to previous messageGo to next message
shree_z
Messages: 75
Registered: February 2008
Member
Quote:
See Thomas' post and tell us if this is what you want.


Yes that is what I need. but I want to update those rows chosen in random on the base table.

Quote:
If you have complex computation then use SELECT FOR UPDATE and loop on it to UPDATE.
If not then directly use UPDATE.


Could you elaborate on this using my code.
begin

open p_rec for

SELECT (some columns) FROM
   (
   select (some columns..)
  from base table
  where (some conditions)
  order by SYS.DBMS_RANDOM.VALUE
   )
WHERE ROWNUM<100

[Updated on: Fri, 07 November 2014 14:33]

Report message to a moderator

Re: Pick random rows [message #627142 is a reply to message #627141] Fri, 07 November 2014 14:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It depends on what you want to update from what.
So can you elaborate your actual need.

Re: Pick random rows [message #627281 is a reply to message #627139] Mon, 10 November 2014 10:02 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
Oracle® Database SQL Language Reference

Sample_clause

The sample_clause lets you instruct the database to select from a random sample of data from the table, rather than from the entire table.

[Updated on: Mon, 10 November 2014 10:03] by Moderator

Report message to a moderator

Re: Pick random rows [message #627282 is a reply to message #627281] Mon, 10 November 2014 10:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Which does NOT change the fact that SAMPLE(100) is invalid, maybe you SHOULD REALLY test what you post before doing so.

Razz

Re: Pick random rows [message #627287 is a reply to message #627282] Mon, 10 November 2014 12:28 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
OK I see it's a percent...
Re: Pick random rows [message #627298 is a reply to message #627287] Mon, 10 November 2014 14:25 Go to previous message
shree_z
Messages: 75
Registered: February 2008
Member
Thanks for all the replies.
Previous Topic: Regular Expression ( REGEXP) not working with Oracle 10.2.0.4.0
Next Topic: Delayed update to global variable via pipelined function?
Goto Forum:
  


Current Time: Thu Apr 25 05:42:46 CDT 2024