Search code examples
sqloracle-databasetop-n

Oracle: I need to select n rows from every k rows of a table


For example: My table has 10000 rows. First I will divide it in 5 sets of 2000(k) rows. Then from each set of 2000 rows I will select only top 100(n) rows. With this approach I am trying to scan some rows of table with a specific pattern.


Solution

  • Assuming you are ordering them 1 - 10000 using some logic and want to output only rows 1-100,2001-2100,4001-4100,etc then you can use the ROWNUM pseudocolumn:

    SELECT *
    FROM   (
      SELECT t.*,
             ROWNUM AS rn            -- Secondly, assign a row number to the ordered rows
      FROM   (
        SELECT *
        FROM   your_table
        ORDER BY your_condition      -- First, order the data
      ) t
    )
    WHERE MOD( rn - 1, 2000 ) < 100; -- Finally, filter the top 100 per 2000.
    

    Or you could use the ROW_NUMBER() analytic function:

    SELECT *
    FROM   (
      SELECT t.*,
             ROW_NUMBER() OVER ( ORDER BY your_condition ) AS rn
      FROM   your_table
    )
    WHERE  MOD( rn - 1, 2000 ) < 100;
    

    Is it possible to increase the set of sample data exponentially. Like 1k, 2k, 4k,8k....and then fetch some rows from these.

    Replace the WHERE clause with:

    WHERE rn - POWER(
                 2,
                 TRUNC( CAST( LOG( 2, CEIL( rn / 1000 ) ) AS NUMBER(20,4) ) )
               ) * 1000 + 1000 <= 100