Solved

Return rows based on date

Posted on 2011-02-11
4
312 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
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now