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: 

Filtering two internal tables and move the non-matching/diff. entries to another table (ABAP 7.4)

sankar1781
Participant
0 Kudos
7,286

Hi Experts,

I have two set of bank data in two internal tables. For ex: It_knbk1 (having 10 entries) and lt_knbk2 (having 4 entries).

I need to filter out and move the non-matching or different between the two to another new internal table. i.e., (the difference of 6 entries).

How to achieve this using inline declaration or ABAP 7.4 commands. I know how to achieve it using our old techniques but would like to use the new ABAP 7.4 commands.

Please help.

Thanks.

1 ACCEPTED SOLUTION

sankar1781
Participant
0 Kudos
6,832

Resolved with the help of sandra.rossi 's answer. Thanks sandra.rossi.

TYPES: begin of ts_knbk,

         kunnr ...
types tt_xknbk type sorted table of ts_knbk ...
types tt_yknbk type sorted table of ts_knbk ...
DATA(xknbk) = VALUE tt_xknbk( ... ).
DATA(yknbk) = VALUE tt_yknbk( ... ).
DATA(gt_del_knbk) = FILTER tt_yknbk( yknbk ... IN xkbnk ... ).
15 REPLIES 15

ThorstenHoefer
Active Contributor
6,832

Hi,

you can check this example: demo_filter_table_condition

https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenfilter_table_condition_abexa.htm

The classic abap could be:

loop at It_knbk1 into wa_1.
read table lt_knbk2 transporting no fields with key table_line = wa_1.
check sy-subrc is not initial.
insert wa_1 into table lt_result.
endloop.

6,832

I guess the visitors would see more the classic part of your answer (which is NOT what the OP asked), than the good FILTER part of your answer (hyperlink).

0 Kudos
6,832

Hi xtrnhfo,

What must be the data type for lt_knbk2?

the table_line doesn't work here as it always considering all the entries between lt_knbk2 and wa_1 are going to sy-subrc is not initial, even though I have a matching entry.

Please let me know.

Thanks.

0 Kudos
6,832

HI,

you can also use single fields as key

for example:

 read table lt_knbk2 transporting no fields with key kunnr = wa_1-kunnr country = wa_1-country.
You can define the internal table as reference to the db table

lt_knbk2 type standard table of yknbk.

juliandanho
Participant
6,832

Hi sankar1781 ,

try this (NEW ABAP):

DATA(lt_knbk_diff) = VALUE knbk_t(
FOR ls_knbk IN COND #( WHEN lines( lt_knbk1 ) >= lines( lt_knbk2 ) THEN lt_knbk1 ELSE lt_knbk2 )
( LINES OF COND #(
WHEN lines( lt_knbk1 ) >= lines( lt_knbk2 ) AND NOT line_exists( lt_knbk2[ table_line = ls_knbk ] ) THEN VALUE #( ( ls_knbk ) )
WHEN lines( lt_knbk2 ) > lines( lt_knbk1 ) AND NOT line_exists( lt_knbk1[ table_line = ls_knbk ] ) THEN VALUE #( ( ls_knbk ) )
) )
).

0 Kudos
6,832

Not sure what NEW means, but saying it works with ABAP 7.40 will make your answer still valid in the future.

0 Kudos
6,832

Hi sandra.rossi

the creator of the question requested a "new" or more recent method for calculating the difference.That's the reasson I wrote "NEW". 7.40 is not strictly new


Kind regards
Julian

0 Kudos
6,832

Okay but your code works with ABAP 7.40 anyway. So why saying "NEW" although your code is "OLD"? 😉

0 Kudos
6,832

Hi julian.danho,

Here the KNBK_T is the type declaration of LT_KNBK*?

Thanks.

Sandra_Rossi
Active Contributor
6,832

Many ways to do it. What did you try?

e.g. via FILTER ... IN or FILTER ... NOT IN ...

sankar1781
Participant
0 Kudos
6,832

Hi sandra.rossi ,

Thanks for your prompt response (always).

I tried as the below image but got 0/null result to gt_del_knbk.

@xtrnhfo, thanks for your response and your answer but I would like to achieve it using ABAP 7.4

@julian.danho, Didn't try it as I thought it can be achieved by using Filter. Let me check yours as well. Thanks a lot.

Sandra_Rossi
Active Contributor
0 Kudos
6,832

I can't answer why you get "0/null result" if you don't share any other information. All depends on input data and actual/expected results which I don't have.

sankar1781
Participant
0 Kudos
6,832

Hi sandra.rossi,

Even I tried with EXCEPT IN using FILTER but no use.

So, other information of data input between two tables are shared here, as a snapshot. Validating based on the KUNNR, COUNTRY and BANK KEY, the difference entry of CITI1 must be fetched between XKNBK and YKNBK.

Hope I answered to your question.

Thanks.

Sandra_Rossi
Active Contributor
6,832

No, sorry, it's not clear. Please create a minimal reproducible program and I'll look at it.

e.g.

TYPES: begin of ts_knbk,
         kunnr ...
types tt_xknbk type sorted table of ts_knbk ...
types tt_yknbk type sorted table of ts_knbk ...
DATA(xknbk) = VALUE tt_xknbk( ... ).
DATA(yknbk) = VALUE tt_yknbk( ... ).
DATA(gt_del_knbk) = FILTER tt_yknbk( yknbk ... IN xkbnk ... ).

sankar1781
Participant
0 Kudos
6,833

Resolved with the help of sandra.rossi 's answer. Thanks sandra.rossi.

TYPES: begin of ts_knbk,

         kunnr ...
types tt_xknbk type sorted table of ts_knbk ...
types tt_yknbk type sorted table of ts_knbk ...
DATA(xknbk) = VALUE tt_xknbk( ... ).
DATA(yknbk) = VALUE tt_yknbk( ... ).
DATA(gt_del_knbk) = FILTER tt_yknbk( yknbk ... IN xkbnk ... ).