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

SUMMING table enteies

Former Member
0 Likes
787

Hi all, I hope someone can help me.

I'm trying to write a report to show production orders and their cost price.

I currently have an internal table with all the production orders and header material & i have another internal table with the BOM components and their cost per component line.

Please could someone tell me how to sum these components against the header material?

please see this code.

FORM get_bom_comp_costs.

  SELECT aa~matnr aa~stlnr bb~idnrk bb~menge
         cc~verpr
    FROM mast AS aa INNER JOIN stpo AS bb ON aa~stlnr  = bb~stlnr
                    INNER JOIN mbew AS cc ON bb~idnrk  = cc~matnr

    INTO wa_bom_comp_costs
    FOR ALL ENTRIES IN it_prod_ord_comps
    WHERE aa~matnr = it_prod_ord_comps-plnbez.

    wa_bom_comp_costs-bomcost = wa_bom_comp_costs-verpr * wa_bom_comp_costs-menge.

    APPEND wa_bom_comp_costs TO it_bom_comp_costs.

  ENDSELECT.

FORM get_prod_ord_costs.

  LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.

    wa_prod_ord_costs-plnbez   = wa_prod_ord_comps-plnbez.
    wa_prod_ord_costs-maktx    = wa_prod_ord_comps-maktx.
    wa_prod_ord_costs-mvgr5    = wa_prod_ord_comps-mvgr5.
    wa_prod_ord_costs-gamng    = wa_prod_ord_comps-gamng.
    wa_prod_ord_costs-aufnr    = wa_prod_ord_comps-aufnr.
    wa_prod_ord_costs-auart    = wa_prod_ord_comps-auart.
    wa_prod_ord_costs-ftrms    = wa_prod_ord_comps-ftrms.
    wa_prod_ord_costs-loekz    = wa_prod_ord_comps-loekz.
    wa_prod_ord_costs-elikz    = wa_prod_ord_comps-elikz.
    wa_prod_ord_costs-asttx    = wa_prod_ord_comps-asttx.
    
    wa_prod_ord_costs-prodord1 = ???????????????????????.
    
    wa_prod_ord_costs-prodord2 = wa_prod_ord_comps-gamng * wa_prod_ord_costs-prodord1.

    APPEND wa_prod_ord_costs TO it_prod_ord_costs.

the prodord1 field needs to look at the header material and sum the cost of the components

many thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
753

Use the below code.

I have done few modifications to your code. Collect statement will do the need for you.

FORM get_prod_ord_costs.
* ------------------------------------------------------------ Begin of changes
  TYPES: BEGIN OF t_tmp_bom_comp_costs,
            matnr TYPE matnr,
            bomcost TYPE bomcost, " Specify the bomcost type - As I am not aware of the type am leavin as just bomtype
         END OF t_tmp_bom_comp_costs.

  DATA: it_tmp_bom_comp_costs TYPE TABLE OF t_tmp_bom_comp_costs,
        wa_tmp_bom_comp_costs TYPE t_tmp_bom_comp_costs.

  LOOP AT it_bom_comp_costs INTO wa_bom_comp_costs.
    wa_tmp_bom_comp_costs-matnr = wa_bom_comp_costs-plnbez.
    wa_tmp_bom_comp_costs-bomcost = wa_bom_comp_costs-bomcost.
* Collect calculates the sum of bomcost using matnr as a key
    COLLECT wa_tmp_bom_comp_costs INTO it_tmp_bom_comp_costs.
  ENDLOOP.
* ------------------------------------------------------------ End of changes


  LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.

    wa_prod_ord_costs-plnbez   = wa_prod_ord_comps-plnbez.
    wa_prod_ord_costs-maktx    = wa_prod_ord_comps-maktx.
    wa_prod_ord_costs-mvgr5    = wa_prod_ord_comps-mvgr5.
    wa_prod_ord_costs-gamng    = wa_prod_ord_comps-gamng.
    wa_prod_ord_costs-aufnr    = wa_prod_ord_comps-aufnr.
    wa_prod_ord_costs-auart    = wa_prod_ord_comps-auart.
    wa_prod_ord_costs-ftrms    = wa_prod_ord_comps-ftrms.
    wa_prod_ord_costs-loekz    = wa_prod_ord_comps-loekz.
    wa_prod_ord_costs-elikz    = wa_prod_ord_comps-elikz.
    wa_prod_ord_costs-asttx    = wa_prod_ord_comps-asttx.
* ------------------------------------------------------------ Begin of changes
    READ TABLE it_tmp_bom_comp_costs INTO wa_tmp_bom_comp_costs WITH KEY matnr = wa_prod_ord_comps-matnr.
    IF sy-subrc = 0.
*----wa_prod_ord_costs-prodord1 = ???????????????????????.
      wa_prod_ord_costs-prodord1 = wa_tmp_bom_comp_costs-bomcost.
    ENDIF.
* ------------------------------------------------------------ End of changes

    wa_prod_ord_costs-prodord2 = wa_prod_ord_comps-gamng * wa_prod_ord_costs-prodord1.

    APPEND wa_prod_ord_costs TO it_prod_ord_costs.
ENDFORM.

Hi all, I hope someone can help me.

I'm trying to write a report to show production orders and their cost price.

I currently have an internal table with all the production orders and header material & i have another internal table with the BOM components and their cost per component line.

Please could someone tell me how to sum these components against the header material?

please see this code.

FORM get_bom_comp_costs.

  SELECT aa~matnr aa~stlnr bb~idnrk bb~menge
         cc~verpr
    FROM mast AS aa INNER JOIN stpo AS bb ON aa~stlnr  = bb~stlnr
                    INNER JOIN mbew AS cc ON bb~idnrk  = cc~matnr

    INTO wa_bom_comp_costs
    FOR ALL ENTRIES IN it_prod_ord_comps
    WHERE aa~matnr = it_prod_ord_comps-plnbez.

    wa_bom_comp_costs-bomcost = wa_bom_comp_costs-verpr * wa_bom_comp_costs-menge.

    APPEND wa_bom_comp_costs TO it_bom_comp_costs.

  ENDSELECT.

FORM get_prod_ord_costs.

  LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.

    wa_prod_ord_costs-plnbez   = wa_prod_ord_comps-plnbez.
    wa_prod_ord_costs-maktx    = wa_prod_ord_comps-maktx.
    wa_prod_ord_costs-mvgr5    = wa_prod_ord_comps-mvgr5.
    wa_prod_ord_costs-gamng    = wa_prod_ord_comps-gamng.
    wa_prod_ord_costs-aufnr    = wa_prod_ord_comps-aufnr.
    wa_prod_ord_costs-auart    = wa_prod_ord_comps-auart.
    wa_prod_ord_costs-ftrms    = wa_prod_ord_comps-ftrms.
    wa_prod_ord_costs-loekz    = wa_prod_ord_comps-loekz.
    wa_prod_ord_costs-elikz    = wa_prod_ord_comps-elikz.
    wa_prod_ord_costs-asttx    = wa_prod_ord_comps-asttx.
    
    wa_prod_ord_costs-prodord1 = ???????????????????????.
    
    wa_prod_ord_costs-prodord2 = wa_prod_ord_comps-gamng * wa_prod_ord_costs-prodord1.

    APPEND wa_prod_ord_costs TO it_prod_ord_costs.

the prodord1 field needs to look at the header material and sum the cost of the components

many thanks

6 REPLIES 6
Read only

former_member191735
Active Contributor
0 Likes
753

>

> Hi all, I hope someone can help me.

>

> I'm trying to write a report to show production orders and their cost price.

>

> I currently have an internal table with all the production orders and header material & i have another internal table with the BOM components and their cost per component line.

>

> Please could someone tell me how to sum these components against the header material?

>

> please see this code.

>

>

FORM get_bom_comp_costs.
> 
>   SELECT aa~matnr aa~stlnr bb~idnrk bb~menge
>          cc~verpr
>     FROM mast AS aa INNER JOIN stpo AS bb ON aa~stlnr  = bb~stlnr
>                     INNER JOIN mbew AS cc ON bb~idnrk  = cc~matnr
> 
>     INTO wa_bom_comp_costs
>     FOR ALL ENTRIES IN it_prod_ord_comps
>     WHERE aa~matnr = it_prod_ord_comps-plnbez.
> 
>     wa_bom_comp_costs-bomcost = wa_bom_comp_costs-verpr * wa_bom_comp_costs-menge.
> 
>     APPEND wa_bom_comp_costs TO it_bom_comp_costs.
> 
>   ENDSELECT.

>

>

>

FORM get_prod_ord_costs.
> 
>   LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.
> 
>     wa_prod_ord_costs-plnbez   = wa_prod_ord_comps-plnbez.
>     wa_prod_ord_costs-maktx    = wa_prod_ord_comps-maktx.
>     wa_prod_ord_costs-mvgr5    = wa_prod_ord_comps-mvgr5.
>     wa_prod_ord_costs-gamng    = wa_prod_ord_comps-gamng.
>     wa_prod_ord_costs-aufnr    = wa_prod_ord_comps-aufnr.
>     wa_prod_ord_costs-auart    = wa_prod_ord_comps-auart.
>     wa_prod_ord_costs-ftrms    = wa_prod_ord_comps-ftrms.
>     wa_prod_ord_costs-loekz    = wa_prod_ord_comps-loekz.
>     wa_prod_ord_costs-elikz    = wa_prod_ord_comps-elikz.
>     wa_prod_ord_costs-asttx    = wa_prod_ord_comps-asttx.
>     
>     wa_prod_ord_costs-prodord1 = ???????????????????????.
>     
>     wa_prod_ord_costs-prodord2 = wa_prod_ord_comps-gamng * wa_prod_ord_costs-prodord1.
> 
>     APPEND wa_prod_ord_costs TO it_prod_ord_costs.

>

> the prodord1 field needs to look at the header material and sum the cost of the components

>

> many thanks

Alright man.

The question is not clear. By the way what do you want to sum? if you want to sum a variable, cant you do like

targetvariable = targetvariable + sourcevariable.

I am not sure what do you want to sum i mean which two variables.

Read only

Former Member
0 Likes
753

Hi, what i'm want to do is.

I want the field with the question marks to look at table it_bom_comp_costs and find all instanaces where wa_prod_ord_comps-plnbez = it_bom_comp_costs-matnr and add up the it_bom_comp_costs-bomcost fields.

basically a vlookup in excel then add them together.

thanks

Read only

RaymondGiuseppi
Active Contributor
0 Likes
753

You need to perform a second LOOP inside the first LOOP.

LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.
  " (...)
  LOOP AT it_bom_comp_costs WHERE matnr = it_prod_ord_comps-plnbez.
    " (...)
  ENDLOOP.
ENDLOOP.

For performance use a SORTED TABLE whith MATNR first key for the second table, or SORT it and manage the LOOP via READ TABLE BINARY SEARCH, LOOP FROM index and EXIT WHEN matnr value change. (look at thread for the second case, for the first case you may need to change the filling of the cost internal table if of sorted type)

Regards,

Raymond

Read only

Former Member
0 Likes
753

thanks for the reply, but how does that sum the lines? I have a table with the lines of the components this sounds like i loop the ones I want to use (the ones that match the header) into another table, then how do i sum them?

Read only

0 Likes
753

Use a [ADD|help.sap.com/abapdocu/en/ABAPADD.htm] statement...

LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.
  CLEAR wa_prod_ord_costs.
  MOVE-CORRESPONDING wa_prod_ord_comps TO wa_prod_ord_costs.
  LOOP AT it_bom_comp_costs INTO wa_bom_comp_costs
    WHERE matnr = it_prod_ord_comps-plnbez.
    ADD wa_bom_comp_costs-bomcost TO wa_prod_ord_costs-prodord1.
  ENDLOOP.
  APPEND wa_prod_ord_costs TO it_prod_ord_costs.
ENDLOOP.

Also take a look at [Online Help at help.sap.com|http://help.sap.com/abapdocu/en/ABENABAP.htm] and [A Freshers Guide to ABAP and SDN|http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/12771%3Fpage%3Dlast%26x-showcontent%3Doff%26x-maxdepth%3D0] [original link is broken] [original link is broken] [original link is broken]; in [Beginner section|http://weblogs.sdn.sap.com/pub/wlg/12771?page=last&x-showcontent=off&x-maxdepth=0] [original link is broken] [original link is broken] [original link is broken];

Regards,

Raymond

Read only

Former Member
0 Likes
754

Use the below code.

I have done few modifications to your code. Collect statement will do the need for you.

FORM get_prod_ord_costs.
* ------------------------------------------------------------ Begin of changes
  TYPES: BEGIN OF t_tmp_bom_comp_costs,
            matnr TYPE matnr,
            bomcost TYPE bomcost, " Specify the bomcost type - As I am not aware of the type am leavin as just bomtype
         END OF t_tmp_bom_comp_costs.

  DATA: it_tmp_bom_comp_costs TYPE TABLE OF t_tmp_bom_comp_costs,
        wa_tmp_bom_comp_costs TYPE t_tmp_bom_comp_costs.

  LOOP AT it_bom_comp_costs INTO wa_bom_comp_costs.
    wa_tmp_bom_comp_costs-matnr = wa_bom_comp_costs-plnbez.
    wa_tmp_bom_comp_costs-bomcost = wa_bom_comp_costs-bomcost.
* Collect calculates the sum of bomcost using matnr as a key
    COLLECT wa_tmp_bom_comp_costs INTO it_tmp_bom_comp_costs.
  ENDLOOP.
* ------------------------------------------------------------ End of changes


  LOOP AT it_prod_ord_comps INTO wa_prod_ord_comps.

    wa_prod_ord_costs-plnbez   = wa_prod_ord_comps-plnbez.
    wa_prod_ord_costs-maktx    = wa_prod_ord_comps-maktx.
    wa_prod_ord_costs-mvgr5    = wa_prod_ord_comps-mvgr5.
    wa_prod_ord_costs-gamng    = wa_prod_ord_comps-gamng.
    wa_prod_ord_costs-aufnr    = wa_prod_ord_comps-aufnr.
    wa_prod_ord_costs-auart    = wa_prod_ord_comps-auart.
    wa_prod_ord_costs-ftrms    = wa_prod_ord_comps-ftrms.
    wa_prod_ord_costs-loekz    = wa_prod_ord_comps-loekz.
    wa_prod_ord_costs-elikz    = wa_prod_ord_comps-elikz.
    wa_prod_ord_costs-asttx    = wa_prod_ord_comps-asttx.
* ------------------------------------------------------------ Begin of changes
    READ TABLE it_tmp_bom_comp_costs INTO wa_tmp_bom_comp_costs WITH KEY matnr = wa_prod_ord_comps-matnr.
    IF sy-subrc = 0.
*----wa_prod_ord_costs-prodord1 = ???????????????????????.
      wa_prod_ord_costs-prodord1 = wa_tmp_bom_comp_costs-bomcost.
    ENDIF.
* ------------------------------------------------------------ End of changes

    wa_prod_ord_costs-prodord2 = wa_prod_ord_comps-gamng * wa_prod_ord_costs-prodord1.

    APPEND wa_prod_ord_costs TO it_prod_ord_costs.
ENDFORM.