Oracle查询第m到第n条数据,用于报表过大的导出

报表产生的过大,一次导出上8万条,就不能导出了。
第一种方法:嵌套select
这种方法是最优方法,因为该方法进行排序后取值,所以能够保证两次取值都会取出相同的值。(不知道,是按什么排序的,验证后,数据是正确的)

最里面的那层select是提取满足要求的所有数据,然后第二层select用于选取前n条数据,最外面的select语句用于选取第m条之后的数据。

#Oracle从目标表中查询第m条到第n条的相应字段
select * from
(select tt., rownum, rn from
(select <想要查询的目标字段>
from 目标表
where 筛选条件) tt
where rownum < n)
where rn > m
取1万条以上的记录:
SELECT nvl(sum(jixiao),0) FROM
(select tt.
, rownum rn from
(SELECT * FROM Sl_Rpt_Performance t where t.Dcoldate=to_date(‘2019-09-22’, ‘yyyy-mm-dd’)and t.pid=‘aaa’
) tt
where rownum < 999999 )
where rn >= 10000;
取1万条以下的记录:
SELECT nvl(sum(jixiao),0) FROM
(select tt., rownum rn from
(SELECT * FROM Sl_Rpt_Performance t where t.Dcoldate=to_date(‘2019-09-22’, ‘yyyy-mm-dd’)and t.pid=‘aaa’/
‘aaa’*/
) tt
where rownum < 10000 )
where rn >= 0;
两个SQL,拼起来是全部的记录。
方法二:
方法的思想是找出前n条数据和前m条数据,然后对两个集合求取差集即可。因为SQL语句的执行顺序问题,order by总是最后执行,所以下方的SQL可以执行,但在任意一个select语句中添加order by会报错。

所以这种方法因为没有按照一定的标准排序后取值,所以有概率两次取值有稍微不同。

select <目标字段> from A where rownum < n
minus
select <目标字段> from A where rownum < m

SELECT nvl(sum(jixiao),0) FROM (
(SELECT * FROM Sl_Rpt_Performance t where t.Dcoldate=to_date(‘2019-09-22’, ‘yyyy-mm-dd’)and t.pid=‘aaa’ AND ROWNUM<=9999999 )MINUS (SELECT * FROM Sl_Rpt_Performance t where t.Dcoldate=to_date(‘2019-09-22’, ‘yyyy-mm-dd’)and t.pid='aaa’AND ROWNUM<10000 ));

发布了37 篇原创文章 · 获赞 2 · 访问量 3231

猜你喜欢

转载自blog.csdn.net/paocai_2019/article/details/102503207