Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 9236

Re: How to add amount of 2 records with same Key fields in an internal table?

$
0
0

Hi Suresh-

Check below sample code:

In my case I have used f1, f2 and f3 fields for comparison.

TYPES: BEGIN OF t_itab,

        f1,

        f2,

        f3,

        f4,

        f5,

        value TYPE i,

        END OF t_itab,

 

        BEGIN OF t_collect,

        f1,

        f2,

        f3,

        value TYPE i,

        END OF t_collect.

 

DATA: it_collect TYPE STANDARD TABLE OF t_collect,

       wa_collect TYPE t_collect,

       it_final TYPE STANDARD TABLE OF t_itab,

       wa_final TYPE t_itab,

       it_final_temp TYPE STANDARD TABLE OF t_itab,

       wa_final_temp TYPE t_itab.

 

CLEAR wa_final.

wa_final-f1 = 'A'.

wa_final-f2 = 'A'.

wa_final-f3 = 'A'.

wa_final-f4 = 'D'.

wa_final-f5 = 'E'.

wa_final-value = 5.

APPEND wa_final TO it_final.

 

CLEAR wa_final.

wa_final-f1 = 'A'.

wa_final-f2 = 'A'.

wa_final-f3 = 'A'.

wa_final-f4 = 'E'.

wa_final-f5 = 'F'.

wa_final-value = 10.

APPEND wa_final TO it_final.

 

CLEAR wa_final.

wa_final-f1 = 'A'.

wa_final-f2 = 'B'.

wa_final-f3 = 'C'.

wa_final-f4 = 'D'.

wa_final-f5 = 'E'.

wa_final-value = 5.

APPEND wa_final TO it_final.

 

SORT it_final BY f1 ASCENDING

                  f2 ASCENDING

                  f3 ASCENDING.

 

WRITE: 'Before Alignment'.

ULINE.

LOOP AT it_final INTO wa_final.

   WRITE: / wa_final-f1, wa_final-f2, wa_final-f3,

            wa_final-f4, wa_final-f5, wa_final-value.

ENDLOOP.

 

LOOP AT it_final INTO wa_final.

   IF ( wa_final-f1 = wa_final-f2 ) AND

      ( wa_final-f2 = wa_final-f3 ).

     CLEAR wa_collect.

     MOVE-CORRESPONDING wa_final TO wa_collect.

     COLLECT wa_collect INTO it_collect.

   ELSE.

     APPEND wa_final TO it_final_temp.

   ENDIF.

ENDLOOP.

 

IF it_collect IS NOT INITIAL.

   LOOP AT it_collect INTO wa_collect.

     READ TABLE it_final INTO wa_final

                         WITH KEY f1 = wa_collect-f1

                                  f2 = wa_collect-f2

                                  f3 = wa_collect-f3.

     IF sy-subrc = 0.

       MOVE-CORRESPONDING wa_collect TO wa_final_temp.

       wa_final_temp-f4 = wa_final-f4.

       wa_final_temp-f5 = wa_final-f5.

       APPEND wa_final_temp TO it_final_temp.

     ENDIF.

   ENDLOOP.

ENDIF.

 

SORT it_final_temp BY f1 ASCENDING

                       f2 ASCENDING

                       f3 ASCENDING

                       f4 ASCENDING

                       f5 ASCENDING.

WRITE: / 'After Alignment'.

ULINE.

LOOP AT it_final_temp INTO wa_final_temp.

   WRITE: / wa_final_temp-f1, wa_final_temp-f2, wa_final_temp-f3,

            wa_final_temp-f4, wa_final_temp-f5, wa_final_temp-value.

ENDLOOP.

 

Output:

Capture.JPG

 

-Venkat


Viewing all articles
Browse latest Browse all 9236

Trending Articles