Solved

Return rows based on date

Posted on 2011-02-11
4
315 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
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 76

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
report returning null 21 80
grouping on time windows 6 43
su - oracle could not open session 6 52
Oracle sql query 7 48
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

920 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

17 Experts available now in Live!

Get 1:1 Help Now