0

I am a beginner in sql and I was wondering if you could help me figure out how to retrieve random records from Oracle. I have the following query:

SELECT p.* from data p where p.id in 
(SELECT DISTINCT t.id   FROM DATA t, comment c, PTV ptv   
WHERE t.code not in ('x','y','Z') 
and t.id = c.id(+)   and ptv.code = t.code   and ptv.code = t.code   
and ptv.version = t.version  AND t.TASK_CODE != 'DRAFT')

How can i select X number of random records from the results returned by the above query? Any suggestions will be appreciated.

6
  • You can simply do it by Join Func. Commented Aug 20, 2014 at 11:13
  • 4
    Unrelated, but: stop using implicit joins in the where clause especially for outer joins (the (+) operator). Use an explicit JOIN or LEFT JOIN operator. Even Oracle recommends to stop using the (+) operator. Commented Aug 20, 2014 at 11:14
  • On a side note. @a_horse_with_no_name In fact, there is nothing wrong with using oracle native(not ANSI) join syntax, it's just a matter of personal preferences to some extent. Does ANSI join provide more clarity, easiness and allows you to write more portable statements? Yes. I personally do prefer ANSI join syntax to the traditional one for that. But at the end of the day ANSI syntax will be transformed to the traditional one by the optimizer, anyway. Commented Aug 20, 2014 at 11:47
  • @NicholasKrasnov: You are right, there is nothing "wrong" using a 20year old syntax that has been superseeded with a newer standard. One advantage of the explicit JOIN operator is that you can't accidentally create a cross join just because you forget the join condition. Plus there are things you can't do with the (+) operator compared to the LEFT JOIN operator (as I said: Oracle recommends to not use (+) any more) Commented Aug 20, 2014 at 11:50
  • @Nicholas Krasnov not true. I was stated by Oracle that some newer optimizer constructs are usable only with new the syntax. (But on contrary - if I remember correctly - somewhere in some special case you can use only (+)). Commented Aug 20, 2014 at 12:22

2 Answers 2

3

The "easy" way to get a random number is to use dbms_random.value or dbms_random.random() and rownum:

with t as (
      <your query here>
     )
select x.*
from (select t.*
      from t
      order by dbms_random.value
     ) x
where rownum < 10;

I suspect there are better ways to write your query. If you want suggestions on that front, write another question, include sample data, desired results, and describe what you want the query to do.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, but what is dbms.random in order by? ORA-00904: "DBMS"."RANDOM": invalid identifier.
2

Use SAMPLE clause:

select * from data t SAMPLE(1) left outer join comment c on (t.id = c.id)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.