Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Filtering data in internal table based on select-option inputs

former_member367551
Participant
0 Likes
6,419

Dear forumers,

I'm in the midst of coding a rather complex data retrieval logics for an ALV report.

In the report, I have 10 select-options (all are with ranges). At one point, I would like to filter the data in an internal table based on the select-option inputs there. All the other data retrievals from this point forward will be based on the filtered internal table (i.e i_tab4 in the codes below).

I've coded this segment in such a way, but there seems to be problems with the read statement here (I think it's rather impossible to code it this way as well because the compiler complained on a syntax error at the IN word used).

if i_tab3 is not initial.

  read i_tab3 into w_wa_3
  with key bukrs IN s_bukrs and
           anln1 IN s_anln1 and
           invnr IN s_invnr and 
           deakt IN s_deakt and 
           prctr IN s_prctr and 
                ...
           matnr IN s_matnr.

  if sy-subrc = 0.
    " Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
  endif.

endif.

Are there any better ways to filter data in an internal table? Please help (as I'm still a newbie). I'd really appreciate any inputs here at all.

Thanks.

13 REPLIES 13
Read only

RaymondGiuseppi
Active Contributor
0 Likes
4,206

You cannit use READ this way, use a LOOP AT

Regards,

Raymond

Read only

0 Likes
4,206

Thanks, Raymond..

Actually, I'm a little hesitant to use the LOOP method (LOOP AT i_tab3 into w_wa_3 where <condition>) because it might just mean using a lot of loops with multiple conditions, thus affecting the program's performance.

What are your thoughts on this?

Read only

Former Member
0 Likes
4,206

Hi,

You can use the Loop and Endloop statmenst to filter some date while you are read the table..

LOOP AT ITAB INTO WA

READ I_TAB3 INTO WA_TAB3 with KEY BUKRS = WA-BUKRS

ANLN = WA-ANLAN........

ENDLOOP.

Like above you can filter the data ver well.

Regards

Thiru

Read only

0 Likes
4,206

Thanks, Thirupathy..

The problem here is, all the 10 select-options have ranges and I need to filter my data in the internal table based on them.

As similar to coding a SELECT statement, I would code "SELECT ... WHERE BUKRS IN S_BUKRS ...". But, isn't "READ TABLE ... WITH KEY BUKRS = S_BUKRS" less accurate here (considering that the S_BUKRS is a range and not a single value)?

Please help.

Read only

Former Member
0 Likes
4,206

it's not posible to use the IN while reading an internal table try somthing like following

if i_tab3 is not initial.
  loop at i_tab3 into w_wa_3
     where bukrs IN s_bukrs and
         and anln1 IN s_anln1 and
         and invnr  IN s_invnr and 
         and deakt IN s_deakt and 
         and prctr  IN s_prctr and 
          .
         and matnr IN s_matnr.
   endloop.
  if sy-subrc = 0.
    " Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
  endif.
 
endif.

Read only

0 Likes
4,206

Code should look like

IF i_tab3 IS NOT INITIAL.
  LOOP AT i_tab3 INTO w_wa_3
    WHERE bukrs IN s_bukrs AND
          anln1 IN s_anln1 AND
          invnr IN s_invnr AND
          deakt IN s_deakt AND
          prctr IN s_prctr AND
          ...
          matnr in s_matnr.
    " Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4
  ENDLOOP.
ENDIF.

- If only first record is required, put an EXIT statement in the LOOP/ENDLOOP block.

- Try to filter the database SELECT before filling the internal table

- if you have several level of LOOP, you may copy internal table, and delete record already processed

- The only optimization in LOOP WHERE comes when the internal table as a sorted type, and the where criteria are the first sequential keys

Regards,

Raymond

Read only

Former Member
0 Likes
4,206

Hi,

Try like this.

delete itab3 where bukrs NOT IN s_bukrs or

anln1 NOT IN s_anln1 or

invnr NOT IN s_invnr or

deakt NOT IN s_deakt or

prctr NOT IN s_prctr or

...

matnr IN s_matnr.

Kiran

Read only

Former Member
0 Likes
4,206

Hi,

At the Select statement level u can sort the data based on above condition.

select * from <table_name> into table i_tab3

where bukrs IN s_bukrs and

anln1 IN s_anln1 and

invnr IN s_invnr and

deakt IN s_deakt and

prctr IN s_prctr and

...

matnr IN s_matnr.

if sy-subrc is initial.

itab4[] = itab3[].

endif.

Read only

Former Member
0 Likes
4,206

Hi Tan,

you can filter by this way.

loop at i_tab3 into w_wa_3 where bukrs IN s_bukrs and

anln1 IN s_anln1 and

invnr IN s_invnr and

deakt IN s_deakt and

prctr IN s_prctr and

...

matnr IN s_matnr.

endloop.

regards,

ravibabu.a

Read only

Former Member
0 Likes
4,206

Hi,

Just practice as below,

DELETE i_tab3 WHERE bukrs IN s_bukrs.

DELETE i_tab3 WHERE anln1 IN s_anln1. etc......

Regards,

Raju.

Read only

Former Member
0 Likes
4,206

Hi ,

if i_tab3 is not initial.

read i_tab3 into w_wa_3

with key bukrs IN s_bukrs and

anln1 IN s_anln1 and

invnr IN s_invnr and

deakt IN s_deakt and

prctr IN s_prctr and

...

matnr IN s_matnr.

if sy-subrc = 0.

" Codes for moving the contents in w_wa_3 to w_wa_4 and finally to i_tab4

endif.

endif.

For this you can try logic as below mention.

append all ranges at selection screen output. then sort this by appropriate field and then use read statement for filtering data.

Hope this is useful for you.

Regards,

Vijay

Read only

0 Likes
4,206

A huge thanks to everyone, for the generous inputs provided here.. I really do appreciate it a lot..

GTREN, Kiran, Ravibabu, I'll definitely consider your suggestions there..

Raymond, thanks for the good tips and advice given.. Really appreciate it as well..

- If only first record is required, put an EXIT statement in the LOOP/ENDLOOP block.

--> All the records are needed, not just the first

- Try to filter the database SELECT before filling the internal table

--> This is done already

- if you have several level of LOOP, you may copy internal table, and delete record already processed

--> Will probably do this too

- The only optimization in LOOP WHERE comes when the internal table as a sorted type, and the where criteria are the first sequential keys

--> Will take note of it

Usha, unfortunately, there are about 7 database tables to select data from, and it isn't as simple / straightforward to just do the data retrieval in one select statement. I have however, filtered the data accordingly in my earlier SELECT statements too.

S Raju, I'll also consider your suggestions, but perhaps by using just one delete statement with multiple conditions in it.

Vijay, this is an interesting way too, will definitely consider your suggestion here as well..

Read only

0 Likes
4,206

Just an update here..

After much tests, I found that the following method works the fastest (and most straight-forward) too:-


    DELETE i_tab3
    WHERE bukrs NOT IN s_bukrs.

    DELETE i_tab3
    WHERE anln1 NOT IN s_anln1.

    DELETE i_tab3
    WHERE invnr NOT IN s_invnr.

    DELETE i_tab3
    WHERE prctr NOT IN s_prctr.

    DELETE i_tab3
    WHERE matnr NOT IN s_matnr.
    
    ...

Thanks once again for all the help and inputs given!