Results 1 to 2 of 2

Thread: SQl Query to select two values or rows randomly - oracle

  1. #1
    Senior Member vijay's Avatar
    Join Date
    May 2012
    Location
    Hyderabad
    Posts
    514

    SQl Query to select two values or rows randomly - oracle

    Please take a look at the following table and data:

    Code:
    value
    -----
    oracle
    mysql
    postgres
    netezza
    mangodb
    My requirement is to select two rows randomly from the above data set. How can i to this by using a query.

  2. #2
    Senior Member vijay's Avatar
    Join Date
    May 2012
    Location
    Hyderabad
    Posts
    514
    Use the DBMS_RANDOM package to get random values.

    Query using rownum:

    Code:
    select *
    from
    (
    select value
    from    tablename
    order by DBMS_RANDOM.RANDOM
    ) a
    where rownum <= 2;

    Query using row number analytic function

    Code:
    select value
    from
    (
    select value, row_number() over(order by DBMS_RANDOM.RANDOM) r
    from   tablename
    ) a
    where r <= 2;

Similar Threads

  1. Replies: 0
    Last Post: 12-14-2012, 09:53 AM
  2. SQL query to Sum values from Duplicated rows
    By Oracleguy in forum Oracle
    Replies: 1
    Last Post: 12-10-2012, 10:46 AM
  3. Replies: 0
    Last Post: 10-30-2012, 08:57 AM
  4. Replies: 1
    Last Post: 06-26-2012, 10:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •