Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Merge the rows in Internal Table

Hello All,

In my Internal table,data is coming in following way

                      YB01       YB02     YB03     YB04

User1               X

User1                               X                         X

User2                               X           X

User2               X

User3               X

My requirement is

                      YB01       YB02     YB03     YB04

User1               X              X                         X

User2               X               X           X

User3               X

I Tried with Collect Query but it didn't work, Because 'X' is char field

Collect Query works with (type I,F and P)

replied

No, it won't. When you use LOOP AT itab INTO wa then you should put MODIFY statement to see changes in itab. When you use ASSIGNING <fs> or REFERENCE INTO it kind of has MODIFY statement in it. In temp table in my code you will have what you need.

TYPES: BEGIN OF ty_data,

   user  TYPE char10,

   field1 TYPE char1,

   field2 TYPE char1,

   field3 TYPE char1,

   END OF ty_data.

   DATA:

         ls_data TYPE ty_data,

         lt_data TYPE TABLE OF ty_data,

         lt_temp TYPE TABLE OF ty_data.

   FIELD-SYMBOLS:

                  <temp> TYPE ty_data,

                  <data> TYPE ty_Data.

   ls_data-user = 'john'.

   ls_data-field1 = 'X'.

   ls_data-field2 = ''.

   ls_data-field3 = ''.

   APPEND ls_data to lt_data.

   ls_data-user = 'john'.

   ls_data-field1 = ''.

   ls_data-field2 = ''.

   ls_data-field3 = 'X'.

   APPEND ls_data to lt_data.

   ls_data-user = 'bob'.

   ls_data-field1 = ''.

   ls_data-field2 = ''.

   ls_data-field3 = ''.

   APPEND ls_data to lt_data.

   ls_data-user = 'bob'.

   ls_data-field1 = 'X'.

   ls_data-field2 = ''.

   ls_data-field3 = 'X'.

   APPEND ls_data to lt_data.

   ls_data-user = 'stan'.

   ls_data-field1 = 'X'.

   ls_data-field2 = 'X'.

   ls_data-field3 = 'X'.

   APPEND ls_data to lt_data.

   lt_temp[] = lt_data[].

   SORT lt_temp by user.

   SORT lt_data by user.

   DELETE ADJACENT DUPLICATES FROM lt_temp COMPARING user.

   LOOP AT lt_temp ASSIGNING <temp>.

     CLEAR: <temp>-field1, <temp>-field2, <temp>-field3.

     LOOP AT lt_data ASSIGNING <data> WHERE user = <temp>-user.

       IF <data>-field1 = 'X'.

         <temp>-field1 = 'X'.

       ENDIF.

       IF <data>-field2 = 'X'.

         <temp>-field2 = 'X'.

       ENDIF.

       IF <data>-field3 = 'X'.

         <temp>-field3 = 'X'.

       ENDIF.

     ENDLOOP.

   ENDLOOP.

I think that is what you wanted.

Message was edited by: Bartlomiej Borucki

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question