oracle rownum分页出现重复数据的情况:
一般情况是分组的字段不唯一导致的
解决办法:
原始sql:(错误的)
SELECT *FROM (SELECT a.*, ROWNUM rFROM hospital_inpatient_medication aWHERE TO_CHAR(discharge_date,'YYYY-MM-DD HH24:MI:SS') BETWEEN '2024-01-01 00:00:00' AND '2024-02-01 00:00:00'AND ROWNUM <= 500ORDER BY discharge_date asc
)
WHERE r >= 1
改进:(正确的)
select *
from (
select row_.*, rownum rownum_
from ( select * from hospital_inpatient_medication
WHERE TO_CHAR(discharge_date,'YYYY-MM-DD HH24:MI:SS') BETWEEN '2024-02-01 00:00:00' AND '2024-03-01 00:00:00'
order by discharge_date asc, id asc )
row_ where rownum <= 5000) b
where b.rownum_ >=1
思路:就是加个唯一键分组