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

Combining lines in internal table with same key....

Former Member
0 Likes
3,806

Hello,

I have a situation as follows where the contents in fields of internal table i_tab[] are as follows:

KUNNR....LEGACY1......LEGACY2.......LEGACY3

1000.........140................167..................178

2000.........310............................................

2000.............................4678......................

3000.........324...............245...................346

I want the records which are coming in two lines (with same KUNNR) to be merged in one line as follows:

KUNNR....LEGACY1......LEGACY2.......LEGACY3

1000.........140................167..................178

2000.........310................ 4678....................

3000.........324...............245...................346

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,744

Hi,

Is Legacy1, legacy2, legacy3 are all character fields or numeric fields?

Do you want to add the similar lines together?

If yes, use collect, else try as follows:



DATA: BEGIN OF itab OCCURS 0,
        kunnr(4),
        leg1(5),
        leg2(5),
     END OF itab.

DATA: itab1 LIKE STANDARD TABLE OF itab WITH HEADER LINE.



itab-kunnr = '1000'    .
itab-leg1 = '1234'.
APPEND itab.
clear itab.
itab-kunnr = '1000'    .
itab-leg2 = '2334'.
APPEND itab.
clear itab.
itab-kunnr = '2000'    .
itab-leg1 = '1264'.
APPEND itab.
clear itab.
itab-kunnr = '2000'    .
itab-leg2 = '23467'.
APPEND itab.


LOOP AT itab.
  WRITE:/ itab-kunnr,
          itab-leg1,
          itab-leg2.
ENDLOOP.

skip 2.
LOOP AT itab.
  AT NEW kunnr.
    itab1 = itab.
    APPEND itab1.
  ENDAT.

  on CHANGE OF itab-leg1.
    if itab-leg1 is not INITIAL.
    itab1-leg1 = itab-leg1.
    modify itab1 TRANSPORTING leg1 where kunnr = itab-kunnr.
    endif.
  endon.

  on CHANGE OF itab-leg2.
    if itab-leg2 is NOT INITIAL.
    itab1-leg2 = itab-leg2.
    modify itab1 TRANSPORTING leg2 where kunnr = itab-kunnr.
    endif.
  endon.
  clear: itab, itab1.
ENDLOOP.

LOOP AT itab1.
  WRITE:/ itab1-kunnr,
          itab1-leg1,
          itab1-leg2.
ENDLOOP.

Regards,

Subramanian

Hello,

I have a situation as follows where the contents in fields of internal table i_tab[] are as follows:

KUNNR....LEGACY1......LEGACY2.......LEGACY3

1000.........140................167..................178

2000.........310............................................

2000.............................4678......................

3000.........324...............245...................346

I want the records which are coming in two lines (with same KUNNR) to be merged in one line as follows:

KUNNR....LEGACY1......LEGACY2.......LEGACY3

1000.........140................167..................178

2000.........310................ 4678....................

3000.........324...............245...................346

5 REPLIES 5
Read only

Former Member
0 Likes
1,744

You can use Collect statement if the non key fields are numerical. Do F1 help on COLLECT.

Read only

ian_maxwell2
Active Participant
0 Likes
1,744

You can use the collect statement (which uses a hash table behing as it's implimentation in order to get good performance). The online help describes this functionality as:

"This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key."

The other way to impliment this if the features of the collect statement don't work properly for you is to create an internal table of type hash and as you insert records always check for an existing record so that you can either update the record or insert the new one. This is more or less how the collect command is implimented.

~Ian

Read only

0 Likes
1,744

If using a hash table the following code is an example of what you can do. The key thing about a hash table is that it provides O(1) performance access as apposed to other access methods. When using a hash table take note of:

- In the definition include "type hashed table" and a unique key

- In the read statements use "with table key"

This example moves records from l_Tab to l_HashTab where l_HashTab is a consolidated version and the legacy values have been added together.

~Ian


types:
  begin of t_FooBar,
    Kunnr type Kna1-Kunnr,
    Legacy1 type N,
    Legacy2 type N,
    Legacy3 type N,
  end of t_FooBar,
  t_hash_FooBar type hashed table of t_FooBar with unique key Kunnr,
  t_tab_FooBar type table of t_FooBar with non-unique default key.


data:
  l_Tab type t_tab_Foobar,
  l_HashTab type t_hash_Foobar.

field-symbols:
  <Record1> type t_Foobar,
  <Record2> type t_Foobar.

loop at l_Tab assigning <Record1>.
  read table l_HashTab
  assigning <Record2>
  with table key Kunnr = <Record1>-Kunnr.
  if sy-subrc = 0.
    <Record2>-Legacy1 = <Record2>-Legacy1 + <Record1>-Legacy1.
    <Record2>-Legacy2 = <Record2>-Legacy2 + <Record1>-Legacy2.
    <Record2>-Legacy3 = <Record2>-Legacy3 + <Record1>-Legacy3.
  else.
    insert <Record1> into table l_HashTab.
  endif.
endloop.

Read only

Former Member
0 Likes
1,745

Hi,

Is Legacy1, legacy2, legacy3 are all character fields or numeric fields?

Do you want to add the similar lines together?

If yes, use collect, else try as follows:



DATA: BEGIN OF itab OCCURS 0,
        kunnr(4),
        leg1(5),
        leg2(5),
     END OF itab.

DATA: itab1 LIKE STANDARD TABLE OF itab WITH HEADER LINE.



itab-kunnr = '1000'    .
itab-leg1 = '1234'.
APPEND itab.
clear itab.
itab-kunnr = '1000'    .
itab-leg2 = '2334'.
APPEND itab.
clear itab.
itab-kunnr = '2000'    .
itab-leg1 = '1264'.
APPEND itab.
clear itab.
itab-kunnr = '2000'    .
itab-leg2 = '23467'.
APPEND itab.


LOOP AT itab.
  WRITE:/ itab-kunnr,
          itab-leg1,
          itab-leg2.
ENDLOOP.

skip 2.
LOOP AT itab.
  AT NEW kunnr.
    itab1 = itab.
    APPEND itab1.
  ENDAT.

  on CHANGE OF itab-leg1.
    if itab-leg1 is not INITIAL.
    itab1-leg1 = itab-leg1.
    modify itab1 TRANSPORTING leg1 where kunnr = itab-kunnr.
    endif.
  endon.

  on CHANGE OF itab-leg2.
    if itab-leg2 is NOT INITIAL.
    itab1-leg2 = itab-leg2.
    modify itab1 TRANSPORTING leg2 where kunnr = itab-kunnr.
    endif.
  endon.
  clear: itab, itab1.
ENDLOOP.

LOOP AT itab1.
  WRITE:/ itab1-kunnr,
          itab1-leg1,
          itab1-leg2.
ENDLOOP.

Regards,

Subramanian

Read only

0 Likes
1,744

> on CHANGE OF itab-leg1.

FYI... Never use the "On Change of" command for this type of processing.

1) It is now considered by SAP to be obsolete

2) Due to the way that it stores it's temporary date it can cause issues especially if used in any sort of nested loops. The SAP online documentation lists it under obsolete now and states " is particularly prone to errors and should be replaced by branches"

If you want to do logic that is based on changes in record values instead of "On Change of" you should either be using:

1) "At" commands, which you also need to be aware of how it looks at and compares fields left to right otherwise you may get undesired results.

2) Explicit "If" statements. But don't forgot to deal with the last record case after the loop.

~Ian