Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Best way to get last N records in the last X days from large table.

user549560Mar 7 2014 — edited Mar 7 2014

Hi! I got a table:

CREATE TABLE SampleTable (

  A_ID                    NUMBER             NOT NULL,

  A_COL_VAL               VARCHAR2(128 BYTE),

  TABLE_INSERT_TIME       TIMESTAMP(2)       DEFAULT SYS_EXTRACT_UTC(SYSTIMESTAMP),

)

TABLESPACE USERS

STORAGE (INITIAL 64 K

         NEXT 1 M

         MAXEXTENTS UNLIMITED)

LOGGING;

Need to get the last N records for the last X days.

What's the most efficient query I can write? The table

has over 2 mln records and I cannot change the design.

  • If there are more than N records in the last X days, only N should be retrieved.
  • If there are fewer records than N in the last X days, all records should be retrieved.

Is subquery the way to go? Rank?

MAX_CNT NUMBER := 5000;

LAST_NUM_DAYS NUMBER:= 150;

Infinite thanks in advance,

This post has been answered by TPD-Opitz on Mar 7 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 4 2014
Added on Mar 7 2014
2 comments
469 views