3 Replies Latest reply: May 7, 2012 8:03 AM by Dave Rabone RSS

    how to select odd/even rows from table....

    vai
      How to select odd/even rows from a table?
      Please help.

      Edited by: vaibhav on May 7, 2012 5:30 AM
        • 1. Re: Even/Odd
          RamGrandhi
          This can be done using the mod function like this:

          For even rows
          select * from (select rownum rn, tableName.* from tableName) where mod(rn,2) = 0;

          For odd rows
          select * from (select rownum rn, tableName.* from tableName) where mod(rn,2) = 1;
          • 2. Re: Even/Odd
            hm
            Does this help?
            select decode(mod(<yourcolumn>,2),0,'EVEN','ODD') oe
            from <yourtable>;
            • 3. Re: how to select odd/even rows from table....
              Dave Rabone
              just don't expect the results to come out in the sequence odd, even, odd, even .....

              The answer you have marked correct needs two order by clauses to guarantee that.

              The inner select will return rows in a random order, potentially different each time you run it. The outer select may not return rows in the same order as the inner one.