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: 

Creating Totals and sub totals

0 Kudos

Hi. I need help in creating totals..my requirement is i have 4 profit centers..the amount should display in each profit center and total of all the profit centers amount should display in the total  amount feiled..how can I acheive this???

13 REPLIES 13

arindam_m
Active Contributor
0 Kudos

Hi,

Check documentation of AT..NEW and SUM keywords you will get an idea. Transaction to check is ABAPDOCU.

Cheers,

Arindam

0 Kudos

Hi Arindam,

In that Docu everything is in Oops, can I get information on Normal ABAP. How to do the totals and to pass the data....

former_member209119
Active Participant
0 Kudos

Hi Naveen,

Loop on itab.

sum = sum + itab-amount.

AT NEW profit_center.

"get the profit center amount

"get the total of all profit centers.

ENDAT.

End loop.

Former Member
0 Kudos

hi,

if you displaying in ALV grid, then

please set property TECH of fieldcatalog for field PRCTR and set Subtotal property for the field amount

i.e.

IF ls_fieldcat-fieldname EQ 'PRCTR'.

    ls_fieldcat-tech      = 'X'.

    ls_fieldcat-no_out    = X'.

ENDIF

IF ls_fieldcat-fieldname EQ 'DMBTR'.

    ls_fieldcat-do_sum    = 'X'.

ENDIF

regards

Tejas

Former Member
0 Kudos

Hi Naveen,

Please note that before using the AT [NEW, END OF] - Control statements, the field which you are using against these statements should be the unique key which is changing in that itab.

          For e.g.; the itab has VBELN and MATNR, and you are using MATNR as AT NEW MATNR inside the LOOP, then the table must be SORT-ed with MATNR and make sure that the VBELN is not changing before MATNR, or in other words, the fields left to MATNR is not changing before MATNR changes. If any field changes before MATNR the control statements will be executed.

Thanks & Regards

Manu S                            

0 Kudos

Can i get sample code for reference.....

0 Kudos

Hi Naveen,

Can you show your itab declaration?

Thanks & Regards

Manu S

0 Kudos

Hi Manu,

Right now it is working fine....need to test the report...if something goes wrong will get back to you.

Thank you very much people for your quick response.

Regards

Naveen Kumar

0 Kudos

Hi naveen kumar

For Sample code see this link for your reference.

http://scn.sap.com/thread/429165

Regards,

Bastin.G

Former Member
0 Kudos

Hi Naveen,

If you are using an OOPS ALV to show your output, then it is much easier.

You can directly use the get_aggregations() method on the fields where you want the subtotals and grand totals;

If you are not using OOPS ALV, then follow as per the messages above. Make sure you sort your internal table on profit center field before you use control break statements.

former_member209119
Active Participant
0 Kudos

Hi,

PFB the sample code for your requirement. Subtotal (profit center wise) and Total amount (For all 4 Profit centers).  you can put the Profit center field for events ' AT END OF...ENDAT' and 'AT LAST OF...ENDAT'.

LOOP AT T_OUTPUT INTO W_OUTPUT1.

    W_OUTPUT = W_OUTPUT1.

     AT NEW VBELN.

*    ULINE.

    WRITE : /02 W_OUTPUT-VBELN,

             20 W_OUTPUT-VKORG,

             35 W_OUTPUT-LDDAT,

             55 W_OUTPUT-KUNNR,

             80 W_OUTPUT-NAME1,

             95 W_OUTPUT-ORT01,

            110 W_OUTPUT-PSTLZ.

     ULINE.

     ENDAT.

*     ULINE.

    WRITE : /02 W_OUTPUT-POSNR,

             20 W_OUTPUT-MATNR,

             36 W_OUTPUT-MATKL,

             45 W_OUTPUT-NTGEW.

*   SUBTOTALS

************************************************

   AT END OF VBELN.

   ULINE.

   SUM.

   WRITE : /30 'SUBTOTAL : ' ,45 W_OUTPUT1-NTGEW.

   ULINE.

   ENDAT.

*   ULINE.

*    GRANDTOTALS

************************************************

   AT LAST.

   SUM.

   WRITE : /35 'GRANDTOTALS :', 50 W_OUTPUT1-NTGEW.

   ENDAT.

   ENDLOOP.

0 Kudos

HI,

LOOP AT IT_VBRK.

     IT_FINAL-VBELN = IT_VBRK-VBELN.

     IT_FINAL-FKDAT = IT_VBRK-FKDAT.

     IT_FINAL-FKART = IT_VBRK-FKDAT.

     READ TABLE IT_VBRP WITH KEY VBELN = IT_VBRK-VBELN.

     IT_FINAL-POSNR = IT_VBRP-POSNR.

     IT_FINAL-MATNR = IT_VBRP-MATNR.

     IT_FINAL-NETWR = IT_VBRK-NETWR.

     IT_FINAL-CESS  = IT_FINAL-NETWR * '.01' .

     IT_FINAL-VAT   = IT_FINAL-NETWR *  '.14'.

     IT_FINAL-CST   = IT_FINAL-NETWR * '.2' .

     IT_FINAL-SUBTOTAL = IT_FINAL-CESS + IT_FINAL-VAT + IT_FINAL-CST.

     IT_FINAL-TOTALL = IT_FINAL-SUBTOTALTOTAL + IT_FINAL-TOTALL.

     APPEND IT_FINAL.

     ENDLOOP.

Former Member
0 Kudos

Hi Naveen,

              have a look at the program given below........it ll get ur doubt cleared.....

TABLES: lips.

TYPES: BEGIN OF ty_lips,

      vbeln TYPE lips-vbeln,

*      posnr TYPE lips-posnr,

       prctr TYPE lips-prctr,

       ntgew TYPE lips-ntgew,

       weight TYPE lips-ntgew,

END OF ty_lips.

TYPES: BEGIN OF ty_lips1,

*      vbeln TYPE lips-vbeln,

*      posnr TYPE lips-posnr,

       prctr TYPE lips-prctr,

       ntgew TYPE lips-ntgew,

       weight TYPE lips-ntgew,

END OF ty_lips1.

TYPES: BEGIN OF ty_vttp,

       tknum TYPE vttp-tknum,

       vbeln TYPE vttp-vbeln,

  END OF ty_vttp.

DATA: gt_lips TYPE TABLE OF ty_lips,

      gs_lips TYPE ty_lips,

      gt_lips1 TYPE TABLE OF ty_lips,

      gs_lips1 TYPE ty_lips1,

      gt_lips2 TYPE TABLE OF ty_lips,

      gs_lips2 TYPE ty_lips,

      gt_lips3 TYPE TABLE OF ty_lips1,

      gt_lips4 TYPE TABLE OF ty_lips1,

      gs_lips4 TYPE ty_lips1,

      v_lips4 TYPE ty_lips,

      l_tabix TYPE sy-tabix,

      gt_vttp TYPE TABLE OF ty_vttp,

      gs_vttp TYPE ty_vttp.

SELECT tknum

       vbeln

       FROM vttp

       INTO  CORRESPONDING FIELDS OF TABLE gt_vttp

       WHERE tknum = '0000001184'.

IF gt_vttp IS NOT INITIAL.

  SELECT  vbeln

          prctr

          ntgew

          FROM  lips

          INTO TABLE gt_lips

    FOR ALL ENTRIES IN gt_vttp

    WHERE vbeln = gt_vttp-vbeln.

ENDIF.

*

*----------this part i had used as no matching data was present in my system--------------------------------

LOOP AT gt_lips INTO gs_lips.

  IF gs_lips-vbeln ='0080013698'.

    gs_lips-prctr = '0000001700'.

  ENDIF.

  IF  gs_lips-vbeln ='0080013699'.

    gs_lips-prctr = '0000001702'.

  ENDIF.

  MODIFY  gt_lips INDEX sy-tabix FROM gs_lips.

*---------------------------------------------------------------------------------------

ENDLOOP.

*select  prctr

**         posnr

*        ntgew

*        from  lips

*        into table gt_lips

*        where  vbeln = '0080004224'.

*

*sort gt_lips by vbeln.

*DELETE GT_LIPS WHERE VBELN = '0080013699'.

DELETE gt_lips WHERE vbeln = '0080013700'.

LOOP AT gt_lips INTO gs_lips.

*  gs_lips1 = gs_lips.

  COLLECT gs_lips INTO gt_lips1.

ENDLOOP.

*-----------------------------

LOOP AT gt_lips1 INTO gs_lips.

*  gs_lips4 = gs_lips.

  MOVE-CORRESPONDING gs_lips TO gs_lips4.

  COLLECT gs_lips4 INTO gt_lips4.

ENDLOOP.

CLEAR gs_lips4.

LOOP AT gt_lips4 INTO gs_lips4.                             "gs_lips1.

  MOVE-CORRESPONDING gs_lips4 TO gs_lips1.

*  gs_lips-ntgew =  gs_lips1-ntgew.

  gs_lips1-prctr = 'Total'.

  COLLECT gs_lips1 INTO gt_lips3.

ENDLOOP.

CLEAR : gs_lips,

        gs_lips1.

CLEAR gs_lips.

APPEND LINES OF gt_lips3 TO gt_lips4.

*READ TABLE gt_lips3 INTO gs_lips1 INDEX sy-tabix.

*MOVE-CORRESPONDING gs_lips1 to gs_lips4.

*APPEND gs_lips TO gt_lips1.

CLEAR: gs_lips1,

*       gs_lips,

       sy-tabix.

READ TABLE gt_lips3 INTO gs_lips1 INDEX 1.

IF sy-subrc EQ 0.

  v_lips4-ntgew = gs_lips1-ntgew.

ENDIF.

CLEAR: gs_lips1.

LOOP AT gt_lips4 INTO gs_lips1.

  l_tabix = sy-tabix.

*  read table gt_lips4 into gs_lips4 with key prctr = gs_lips1-prctr.

  IF gs_lips1-prctr NE 'Total'.

    gs_lips1-weight =  100 * ( gs_lips1-ntgew / v_lips4-ntgew ) .

  ELSEIF gs_lips1-prctr EQ 'Total'.

    gs_lips1-weight =  100.

  ENDIF.

  MODIFY gt_lips4 INDEX l_tabix FROM gs_lips1 TRANSPORTING weight.

  CLEAR sy-tabix.

ENDLOOP.

REFRESH gt_lips3.

CLEAR : gs_lips2,gs_lips1,gs_lips.

gt_lips3 = gt_lips4.

*----------------------------------------------------------

LOOP AT gt_lips3 INTO gs_lips1.

  WRITE:  / gs_lips1-prctr COLOR 7,

           gs_lips1-ntgew COLOR 1,

           gs_lips1-weight COLOR 3.

ENDLOOP