Solved

Return rows based on date

Posted on 2011-02-11
4
319 Views
Last Modified: 2012-06-27
Hello

Sample data

COLA     COLB       COLC
Cpy A    AA         01/02/2010
Cpy A    AA         08/05/2010
Cpy A    AA         11/02/2009
Cpy A    AA         01/02/2011
Cpy B    AAB        21/02/2011
Cpy B    AA         08/05/2010
Cpy B    AAB        21/03/2008
Cpy B    AAZ        19/02/2010

 
variable :date
for this example assume the date is '20/01/2010'

I now want a query to run and then only select rows
where COLC date field equals the date variable and if not
to return the first date nearest to it (before & not after)
So, my returned data would now look as follows given variable date
'20/01/2010'

COLA     COLB       COLC
Cpy A    AA         11/02/2009
Cpy B    AAB        21/03/2008

Is this possible
0
Comment
Question by:philsivyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 400 total points
ID: 34869591
select * from tbl where colc = (select max(t.colc) from tbl t where t.colc <= 'your date here');
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34869657
try this :

select * from your_table where colc = '20-jan-2010'
union
select * from your_table x where x.colc < '20-jan-2010'
and not exists ( select 1 from your_table y where y.colc = '20-jan-2010' )
0
 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 100 total points
ID: 34869669
just modified a bit more to use the cola as you need one date for each value of cola right ?

select * from your_table where colc = '20-jan-2010'
union
select * from your_table x where x.colc < '20-jan-2010'
and not exists ( select 1 from your_table y where y.colc = '20-jan-2010' and x.cola = y.cola)
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34870917
You closed this to quick.

This should be much more efficient since it only hits the table once.

select colA, colB, colC from (
      select colA, colB, colC, row_number() over(partition by cola order by colC desc) myrownum
      from tab1 where colC <= to_date('20/01/2010','DD/MM/YYYY')
)
where myrownum = 1
/
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question