Oracle paging query of duplicate data

Source: Internet
Author: User

The Oracle paging query is generally implemented as follows:
Select * from (
Select a. *, rownum r from (
Your own SQL
) A where rownum <=?
) Where r>?

If this SQL statement is sorted by a field containing repeated values

Duplicate records will be found on different pages, and some records cannot be found (not necessarily)

The test code is as follows:

Create table test (
ID VARCHAR2 (10) not null,
OD VARCHAR2 (10) NULL
)
GO

-- Insert Test Data
Insert into TEST
Select level, case when level <= 3 then level else null end from dual connect by level <= 10
Go


-- Test paging results
Article 1-5
Select * from (
Select a. *, rownum r from (
Select t1.id from TEST t1
Order by t1.od desc
) A where rownum <= 5
) Where r> 0
Returned results
Row ID R
1 4 1
2 8 2
3 7 3
4 6 4
5 5 5

Article 6-10
Select * from (
Select a. *, rownum r from (
Select t1.id from TEST t1
Order by t1.od desc
) A where rownum <= 10
) Where r> 5
Returned results
Row ID R
1 6 6
2 5 7
3 3 8
4 2 9
5 1 10

We can see that id = 5 and id = 6 in the first result set have not found the records with id = 9 and id = 10 at the same time.

The solution is as follows:
If the preceding two conditions are met
Add the primary key of the table after order;
In this example
Change to order by t1.od desc, And the phenomenon disappears after t1.id

If there is a better solution, I hope to inform =

For more information about Oracle, see Oracle topics page http://www.bkjia.com/topicnews.aspx? Tid = 12

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.