Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Return rows less than value and the next "bigger" value row

kron777Feb 8 2012 — edited Feb 9 2012
I'm on 10gR2

Trying my best here to do everything in a single SQL rather than write PL/SQL. I know it's possible, i'm just not that good at SQL past oracle 8i! I've searched the forums and have found lots of examples for date ranges using connect by and level, but nothing specifically relating to value ranges.

Basically, i've got a table with employee numbers and approval limits:
  with  a as (select 12345 emp_no,1000 val from dual 
                 UNION select 12346,5000 from dual UNION select 12347,9999 from dual)
  select * from a 
returns
EMP_NO  VAL
------- ---------------
12345   1000
12346   5000
12347   9999
I'd like an SQL statement which returns all rows less than the &parameter but also the row of the "child" who as a value "bigger" than the greatest value of the value less than the parameter.

It's probably easier to show examples.

If &parameter was 2342 then show
EMP_NO  VAL
------- ---------------
12345   1000
12346   5000
If &parameter was 7898 then show
EMP_NO  VAL
------- ---------------
12345   1000
12346   5000
12347   9999
If &parameter was 10 then show
EMP_NO  VAL
------- ---------------
12345   1000
If &parameter was 788777 then show
No rows returned 
Essentially I'm trying to return the approval chain that an invoice will have to go through before it can be approved up to the &parameter amount.

Tried with connect by, even lead function. Requesting assistance from the gurus to get it right. Hope this might also assist others.

Edited by: user13007502 on 08-Feb-2012 04:56

Edited by: user13007502 on 08-Feb-2012 04:58
This post has been answered by Frank Kulash on Feb 8 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 8 2012
Added on Feb 8 2012
14 comments
2,395 views