Home > Ask the Oracle Experts > (Archive) App Dev: PL/SQL and XML Questions & Answers > Query for five most recent dates
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Query for five most recent dates

Lewis Cunningham EXPERT RESPONSE FROM: Lewis Cunningham

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 08 December 2005
I have a table that I want to query by a char date field. I want to get the five most recent dates. So far using MAX in a subquery I can get one row. How do I get the rest? Here is an example:
Select ky_ba,......,dt_prcs
from my_table
where ky_ba = 999999999 and dt_prcs =
   ( Select MAX(dt_prcs) from my_table where ky_ba = 999999999 );

>
EXPERT RESPONSE

I see where you say you have a char date field. In my example, I will use a date date field. I'd suggest you change your schema to do the same. If you can't, wrap a to_date(dt_prcs, '') around it.

First, what happens if you have six that have an equal max date? Just return five randomly? Return all? I will guess you want to always return only five and you have no additional criteria to rank them. The easiest way to do this is with the row_number() analytic function.

Here is a complete example:

create table my_table (
  ky_ba number,
  some_data varchar2(10),
  some_more_data number,
  dt_prcs date );
  
insert into my_table values (999999999, 'aaa', 9999, to_date( '01-jan-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'bbb', 8888, to_date( '01-feb-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'ccc', 7777, to_date( '01-mar-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'ddd', 6666, to_date( '01-apr-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'eee', 5555, to_date( '01-may-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'fff', 4444, to_date( '01-jun-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'ggg', 3333, to_date( '01-jul-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'hhh', 2222, to_date( '01-aug-2005', 'DD-MON-YYYY') );
insert into my_table values (999999999, 'iii', 1111, to_date( '01-sep-2005', 'DD-MON-YYYY') );
insert into my_table values (888888888, 'jjj', 0000, to_date( '01-oct-2005', 'DD-MON-YYYY') );

commit;


select ky_ba, some_data, some_more_data, dt_prcs
  from (   
    Select ky_ba, some_data, some_more_data, dt_prcs,
           row_number() over(partition by ky_ba order by dt_prcs desc) rn
      from my_table )
  where ky_ba = 999999999   
    and rn <= 5;
Row_number() is a great way to return the top(n) or bottom(n) values. For further reading, check out the rank() and dense_rank() functions in the documentation.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
(Archive) App Dev: PL/SQL and XML
Detect if a column is NUMBER or VARCHAR2 in PL/SQL
Loading data into Oracle
Log execution time for a procedure
Hiding objects in a schema
Connecting with Visual Basic
SQL ANSI standards and compliance
Developing a parser for recognizing HTML tags
Oracle and SOAP
Concatenating XML fragments
Two schemas on a single database

Oracle PL/SQL
Oracle's free SQL Developer adds database migration tool
Confused about Oracle certification exams
ORA-01422 error when procedure returns more than one row
Calling procedure inside another procedure in anonymous block
How to import comma-delimited text file to Oracle table
Oracle updates Microsoft developer tools
PLS-00103 errors
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
PL/SQL  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts