0

I'm reading a table from a Java app and now I wanted to split this task across two different apps each processing its own rows.

I tried to use this

Select * from emp where rownum between 0 and (Select count(*) from emp)/2;

I need your suggestion on the best way to read the next half of the table.

I tried

Select * from emp where rownum between ((Select count(*) from emp)/2)+1 and (Select count(*) from emp);

but it didn't give me results ( gave me a empty set of records )

amdixon
  • 3,814
  • 8
  • 25
  • 34
Jeeppp
  • 1,553
  • 3
  • 17
  • 39

2 Answers2

0

rownum is assigned to the rows after the result was fetched, so you can't use it as you did. Instead you can do something like:

   select *
   from (select rownum r, e.* from emp e)
   where r between ((Select count(*) from emp)/2)+1 and (Select count(*) from emp);
Grisha Weintraub
  • 7,803
  • 1
  • 25
  • 45
0

You can also interleave the rows

Assuming sequential id:

App 1: Select * from emp where mod(emp_id, 2) = 0;

App 2: Select * from emp where mod(emp_id, 2) = 1;

or

App 1:

WITH emp_ordered AS (Select * from emp order by emp_id)
Select * FROM (Select emp.*, rownum rn from emp_ordered) where mod(rn, 2) = 0;

App 2:

WITH emp_ordered AS (Select * from emp order by emp_id)
Select * FROM (Select emp.*, rownum rn from emp_ordered) where mod(rn, 2) = 1;
Husqvik
  • 5,669
  • 1
  • 19
  • 29