Oracle 分页

阅读更多
详解Oracle的几种分页查询语句: http://database.51cto.com/art/200904/118737.htm
ORACLE大数据量下的分页解决方法: http://ddkangfu.blog.51cto.com/311989/1425797/
oracle 分页数据重复数据不正确: https://my.oschina.net/u/852445/blog/402325
实例:
SELECT * 
			FROM
			(
			SELECT RS.AMOUNT,RS.AREA,RS.AREANAME,[color=red]ROWNUM rn
			FROM
			(
			SELECT (
			CASE 
			WHEN oy.AMOUNT IS NULL THEN
			    ny.AMOUNT-0
			WHEN ny.AMOUNT IS NULL THEN
			    0-oy.AMOUNT
			ELSE
			   ny.AMOUNT-oy.AMOUNT
			END) AS amount,NY.AREA,NY.areaName
			FROM
			(SELECT "SUM"(VI.AMOUNT) AS AMOUNT ,VI.AREA, dm.AREA_NAME as areaName FROM VI_INCOME_ITEM_MAPPING VI 
			LEFT JOIN DM_AREA dm ON VI.AREA = dm.AREA_CODE
			WHERE VI.T_ITEM =  #{BudgetTItem,jdbcType=VARCHAR} AND VI.BLEVEL ='3'
			AND "SUBSTR"(VI.TXN_DATE,1,6) = ]]>  #{startMonth,jdbcType=VARCHAR}
		    AND "SUBSTR"(VI.TXN_DATE,1,6)    #{endMonth,jdbcType=VARCHAR}
			GROUP BY VI.AREA,dm.AREA_NAME) ny
			LEFT JOIN 
			(SELECT "SUM"(VI.AMOUNT) AS AMOUNT ,VI.AREA, dm.AREA_NAME as areaName FROM VI_INCOME_ITEM_MAPPING VI 
			LEFT JOIN DM_AREA dm ON VI.AREA = dm.AREA_CODE
			WHERE VI.T_ITEM = #{BudgetTItem,jdbcType=VARCHAR} AND VI.BLEVEL ='3'
			AND "SUBSTR"(VI.TXN_DATE,1,6) = ]]> #{lastStartMonth,jdbcType=VARCHAR} 
			AND "SUBSTR"(VI.TXN_DATE,1,6)   #{lastEndMonth,jdbcType=VARCHAR} 
			GROUP BY VI.AREA,dm.AREA_NAME) oy
			ON NY.AREA = OY.AREA
			ORDER BY amount DESC ) [color=red]rs WHERE ROWNUM <= 10) WHERE rn >0


注意红色字体内层用ROWNUM,外层用RN,Oracle 一般第一次select时,有ROWNUM,可在第二层分页

分页两种方法:
第一种:
SELECT * FROM (SELECT t.*,ROWNUM as rowno FROM (SELECT * FROM test ORDER BY ADD_TIME ) t )
 WHERE rowno  BETWEEN 200001 AND 300000;

第二种:
SELECT * FROM ( SELECT t.*,ROWNUM as rowno FROM (SELECT * FROM test ORDER BY ADD_TIME) t WHERE ROWNUM  <= 300000)
WHERE rowno >=200001

第二种比第一种有效,因为第二种锁定了分页结果集,而第一种是从表所有记录中,分页;
对于两层嵌套的分页查询如:
实例:
select (select * from test ORDER BY ADD_TIME) where ROWNUM between 100000 and 200000

对于含排序(ORDER BY ADD_TIME), 则排序字段ADD_TIME字段必须是唯一索引,否则已出现
结果集为空,或数据重复,含排序的正确做法如上面两种分页方式,再加一层嵌套。




你可能感兴趣的:(oracle)