Home » SQL & PL/SQL » SQL & PL/SQL » How to get the first less date than agiven date in sql
How to get the first less date than agiven date in sql [message #647314] Tue, 26 January 2016 11:09 Go to next message
blyzz
Messages: 10
Registered: October 2015
Junior Member
I have a query where I have have 2 dates surgdt and movedt and I have to find the first surgdt which is <= movedt.

So if I have dates like
Surgdt Movedt

10/9/2015 10/12/2015
10/12/2015 10/23/2015
10/15/2015
If my query is like
Select * from tblA where surgdt<=Movedt
For 10/12/2015 movedt I am getting 2 surgdt 10/9/2015 and 10/12/2015
And for 10/23/2015 MoveDt , I am getting all 3 surgdt where I need only the last Surgdt which is 10/15/2015. How can I get the last Surgdt <= Movedt in oracle sql?
Re: How to get the first less date than agiven date in sql [message #647315 is a reply to message #647314] Tue, 26 January 2016 11:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read
Re: How to get the first less date than agiven date in sql [message #647320 is a reply to message #647314] Tue, 26 January 2016 12:04 Go to previous message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Your data and requirements are unclear.

If you have something like this:
SCOTT@orcl> SELECT * FROM tbla
  2  /

SURGDT     MOVEDT
---------- ----------
10/09/2015 10/12/2015
10/12/2015 10/12/2015
10/09/2015 10/23/2015
10/12/2015 10/23/2015
10/15/2015 10/23/2015

5 rows selected.


-- and you are doing this, which is not getting what you want:
SCOTT@orcl> Select * from tblA where surgdt<=Movedt
  2  /

SURGDT     MOVEDT
---------- ----------
10/09/2015 10/12/2015
10/12/2015 10/12/2015
10/09/2015 10/23/2015
10/12/2015 10/23/2015
10/15/2015 10/23/2015

5 rows selected.


-- it appears that you want something like this:
SCOTT@orcl> SELECT MAX (surgdt) AS surgdt, movedt
  2  FROM   tbla
  3  WHERE  surgdt <= movedt
  4  GROUP  BY movedt
  5  /

SURGDT     MOVEDT
---------- ----------
10/12/2015 10/12/2015
10/15/2015 10/23/2015

2 rows selected.

Previous Topic: Corrections with overlapping time frames (SOLVED)
Next Topic: SQL QUERY HELP
Goto Forum:
  


Current Time: Tue Apr 23 06:17:29 CDT 2024