Application Development 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: 

Sum of fields in Internal Table

Former Member
0 Kudos

Hi Gurus,

I have an internal table which has fields Matnr, Plant and Valuation Class and Stock.

I need to find sum of stock for a combination of Material and Plant.

Please let me know how can i implement it.

Matnr Werks Val Class Stock

----


1 W1 V1 200

1 W1 V2 300

2 W2 V3 500

2 W2 V1 700

I should get the sum of Matnr '1' and Werks 'W1' combination as as 200 + 300 = 500

Regards,

Avi...

7 REPLIES 7

Former Member
0 Kudos

sort itab.

loop at itab into wa.

collect wa into itab 1.

endloop.

Former Member
0 Kudos

Hi,

One way would be to build a second internal table but use the collect statement instead of append to insert the entries. This will sum all numeric fields using all character fields as the keys.

Regards,

Darren

0 Kudos

The table has other numerical fields too

former_member705122
Active Contributor
0 Kudos

Hi,

loop at itab.
wa_sum = wa_sum + itab-<field>.            "here <field> is stock
wa_matnr = itab-matnr.
at new matnr.
write: 
  wa_matnr
  wa_sum.
clear: 
  wa_matnr,
  wa_sum.
endat.
endloop.

former_member181995
Active Contributor
0 Kudos

initially

1 W1 V1 200
1 W1 V2 300
2 W2 V3 500
2 W2 V1 700

First Concatenate Matnr and Werks and take it in some temp and than your table would be some looks like

A1 W1 V1 200"A1 is concatenate result of matnr and werks
A1 W1 V2 300
A2 W2 V3 500
A2 W2 V1 700

sort itab by temp.
loop at itab.
at end of temp"concatenate field
sum.
endat.
endloop.

Amit

Former Member
0 Kudos

Hello Avi,

I recently had an issue exactly like this.

Here is the solution I used.

LOOP AT i_tab INTO wa_tab.

MOVE wa_tab TO l_wa_tab.

AT NEW werks.

CLEAR: p_stock.

ENDAT.

p_stock = p_stock + l_wa_tab-stock.

AT END OF werks.

wa_tab-stock = p_stock.

MODIFY i_tab FROM wa_tab TRANSPORTING stock

WHERE matnr = l_wa_tab-matnr

AND werks = l_wa_tab-werks.

ENDAT.

ENDLOOP.

Delete adjacent duplicates from i_tab comparing matnr werks.

The result is i_tab having a stock field with the total stock for each plant and material combination.

Thus the first two entries on your table would have stock = 500.

To get the unique plant and material combination with total stock just do a delete or you can modify another table from the work area.

Regards,

C

former_member212653
Active Contributor
0 Kudos

> Check out this code:

>

>


> *&---------------------------------------------------------------------*
> *& Report  ZTEST3
> *&
> *&---------------------------------------------------------------------*
> *&
> *&
> *&---------------------------------------------------------------------*
> 
> REPORT  ztest3.
> TYPES:
> BEGIN OF x_employee,
>   id TYPE n LENGTH 10,
>   name TYPE c LENGTH 20,
>   group TYPE c LENGTH 4,
> END OF x_employee,
> BEGIN OF x_emp_sal,
>   id TYPE n LENGTH 10,
>   salary TYPE p LENGTH 16 DECIMALS 2,
> END OF x_emp_sal,
> BEGIN OF x_final,
>   group TYPE c LENGTH 4,
>   id TYPE n LENGTH 10,
>   name TYPE c LENGTH 20,
>   salary TYPE p LENGTH 16 DECIMALS 2,
> END OF x_final.
> 
> DATA:
> i_employee TYPE STANDARD TABLE OF x_employee INITIAL SIZE 0,
> i_final TYPE STANDARD TABLE OF x_final INITIAL SIZE 0,
> i_salary TYPE STANDARD TABLE OF x_emp_sal INITIAL SIZE 0,
> wa_employee TYPE x_employee,
> wa_salary TYPE x_emp_sal,
> wa_final TYPE x_final,
> wa_final_temp TYPE x_final.
> 
> DEFINE append_employee.
>   wa_employee-id = &1.
>   wa_employee-name = &2.
>   wa_employee-group = &3.
>   append wa_employee to i_employee.
> END-OF-DEFINITION.
> DEFINE append_salary.
>   wa_salary-id = &1.
>   wa_salary-salary = &2.
>   append wa_salary to i_salary.
> END-OF-DEFINITION.
> 
> append_employee:
> 1 'John' 'ENG',
> 2 'Mary' 'ENG',
> 3 'Pooja' 'IT',
> 4 'Payal' 'IT',
> 5 'Sourav' 'IT'.
> 
> append_salary:
> 1 '111.00',
> 2 '1111.00',
> 3 '11111.00',
> 4 '111111.00',
> 5 '1111111.00'.
> 
> LOOP AT i_employee INTO wa_employee.
>   READ TABLE i_salary INTO wa_salary
>    WITH KEY id = wa_employee-id.
>   IF sy-subrc = 0.
>     wa_final-id = wa_employee-id.
>     wa_final-name = wa_employee-name.
>     wa_final-group = wa_employee-group.
>     wa_final-salary = wa_salary-salary.
>     APPEND wa_final TO i_final.
>   ENDIF.
> ENDLOOP.
> 
> SORT i_final BY group.
> 
> LOOP AT i_final INTO wa_final_temp.
>   wa_final = wa_final_temp.
>   AT END OF group.
>     SUM.
>     WRITE: /1 wa_final_temp-group, 5 wa_final_temp-salary.
>   ENDAT.
> ENDLOOP.
> 
> 

>

> output will be:

>


> ENG                         1.222,00   
> IT                      1.233.333,00   
> 

>

>