SQL分割总行数

时间:2015-12-31 07:57:20

标签: sql oracle

我正在从Java应用程序中读取表格,现在我想将这个任务分成两个不同的应用程序,每个应用程序都处理自己的行。

我试着用这个

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

我需要你的最佳方式来阅读下半部分的建议。

我试过

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

但它没有给我结果(给了我一套空的记录)

2 个答案:

答案 0 :(得分:0)

在获取结果后,

rownum被分配给行,因此您无法像以前那样使用它。相反,你可以做类似的事情:

   select *
   from (select rownum r, e.* from emp e)
   where r between ((Select count(*) from emp)/2)+1 and (Select count(*) from emp);

答案 1 :(得分:0)

您还可以交错行

假设顺序id:

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

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

应用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;