Skip to Main Content
  • Questions
  • Find gl code combinations that falls between a range of values defined by a lookup table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Rupa.

Asked: September 08, 2015 - 7:01 pm UTC

Last updated: September 14, 2015 - 4:10 pm UTC

Version: 11g

Viewed 1000+ times

You Asked

I need to write a query that selects all gl code combinations
Table - gl_code_combinations (Oracle ERP Suite 12)

where the segment5 of the table falls with a range of values

select * from gl_code_combinations where
enabled_flag='Y'
AND account_type in ('R','A','L')
AND ((segment5 between '41000' and '43999')
OR (segment5 between '11000' and '11999')
OR (segment5 between '14000' and '15000')
OR (segment5 between '28000' and '30000')
OR (segment5 = '12400')
OR (segment5 = '44300')
OR (segment5 = '49990' ))

I dont want to have this hardcoded and would like to use a lookup table that stores the ranges and the values
Range - 11000 to 11999
Range 2 - 14000 to 150000
Range 3 - 28000 to 30000
Value1 12400
Value2 44300
Value3 49990


How can i do this..

and Connor said...

Is this what you had in mind ?

SQL> drop table GL purge;

Table dropped.

SQL>
SQL> create table GL
  2  as select rownum*10 x
  3  from dual
  4  connect by level <= 10000;

Table created.

SQL>
SQL> drop table LOOKUP purge;

Table dropped.

SQL>
SQL> create table LOOKUP ( lo int, hi int);

Table created.

SQL>
SQL>
SQL> insert into LOOKUP values (11000 , 11999 );

1 row created.

SQL> insert into LOOKUP values (14000 , 15000);

1 row created.

SQL> insert into LOOKUP values (28000 , 30000);

1 row created.

SQL> insert into LOOKUP values (12400, 12400);

1 row created.

SQL> insert into LOOKUP values (44300, 44300);

1 row created.

SQL> insert into LOOKUP values (49990, 49990 );

1 row created.

SQL>
SQL> select gl.*
  2  from GL, LOOKUP
  3  where gl.x between LOOKUP.lo and LOOKUP.hi;

         X
----------
     11000
     11010
     11020
     11030
     11040
     11050
     11060
     11070
     11080
[snip]
     29950
     29960
     29970
     29980
     29990
     30000
     44300
     49990

405 rows selected.

SQL>
SQL>



Is this answer out of date? If it is, please let us know via a Comment