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

sum in internal table.

Former Member
0 Likes
3,719

Dear All

i have one internal table . In that table materialcode, qty,movementtype fields are available. i want to get sum of the qty based on materialcode and movermenttype.

Thanks and Regards

Suresh

Dear All

i have one internal table . In that table materialcode, qty,movementtype fields are available. i want to get sum of the qty based on materialcode and movermenttype.

Thanks and Regards

Suresh

13 REPLIES 13
Read only

Former Member
0 Likes
2,504

Hi,

Try the following code

loop at itab1 into wa.

collect itab2 from wa.

endloop.

L.Velu

Read only

0 Likes
2,504

Hi all

Pls Any one give one example

Regards

Suresh

Read only

rodrigo_paisante3
Active Contributor
0 Likes
2,504

Hi, try to use the collect statement. You will get the sum with this.

Rodrigo

Read only

abdulazeez12
Active Contributor
0 Likes
2,504

use COLLECT statememt while passing the contents of the final internal table to another table, say, itab2. The results will be in the internal table itab2.

Read only

Former Member
0 Likes
2,504

Hi,


SELECT <materialcode> <qty> <movementtype> INTO CORRESPONDING FIELDS OF itab FROM <tab> WHERE <cond>

COLLECT itab.  "instead of append use collect

ENDSELECT. 

"OR you can write like this also
"after collecting data into itab.
LOOP AT itab.
COLLECT itab INTO itab2. 
ENDLOOP.

Message was edited by:

Perez C

Read only

Former Member
0 Likes
2,504

Hi Suresh,

SORT internal table based on material number and movement type and use control break statements AT END OF to get the sum of the qty based on material number and movement type.

SORT ITAB BY MATERIALCODE MOVEMENTTYPE.

LOOP AT ITAB.

AT END OF MOVEMENTTYPE.

SUM. -> This will have sum based on material type and movement type

ENDAT.

ENDLOOP.

Thanks,

Vinay

Read only

0 Likes
2,504

Hi Vinaykumar,

this will work only if the structure of the internal table starts with the fields MATERIALCODE and MOVEMENTTYPE.

Then the SORT statement does not need the BY addition.

Look for F1 on AT END OF. Although only few people seem to understand why it is as it is.

Regards,

Clemens

Read only

0 Likes
2,504

hi Vinay

Thanks for replay the following code i am using i want total qty materialcode and movementtype wise

loop at IntTbl.

AT NEW MATNR.

AT NEW BWART.

sum.

ENDAT.

write :at / sy-vline, 2(15) IntTbl-matnr,

17 sy-vline, 18(30) IntTbl-bwart,

48 sy-vline, 49(15) IntTbl-erfmg,

75 sy-vline.

endat.

endloop.

Thanks

Suresh

Read only

0 Likes
2,504

Hi suresh,

although I'm convinced it is very good to know how to apply the control break statements like AT NEW and so on, I'm convinced too that it is not worth the time to waste on it if the Goal is a simple list. SAP herself would never use anything except ALV for something like this.

Look at the below coding, just 45 lines for everything. It displays the results you need in a nice ALV grid. When the Grid is displayed, put the mouse on quantity and then push the TOTAL button above. After that, mark columns BWART and MATNR and push the SUBTOTALS button (it appears after TOTAL is requested).

And you will see that the ALV will sum up totals for every quantity unit used because the field catalog establishes the reference from quantity to unit field..

That's it.

You may find out yourself as how to save that as a default ALV layout.


*&---------------------------------------------------------------------*
*&      Form  bdisp
*&---------------------------------------------------------------------*
FORM bdisp.
  DATA:
    BEGIN OF ls_inttbl,
    matnr TYPE mseg-matnr,
    bwart TYPE mseg-bwart,
    erfmg TYPE mseg-erfmg,
    erfme TYPE mseg-erfme,
    END OF ls_inttbl,
    lt_inttbl LIKE TABLE OF ls_inttbl,
    lt_fieldcat TYPE  slis_t_fieldcat_alv with header line.

* get data
  SELECT matnr bwart erfmg erfme INTO TABLE lt_inttbl
    FROM mseg
    WHERE matnr IN s_matnr.
* get field catalog with (at least) used fields
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name = 'MSEG'
       CHANGING
            ct_fieldcat      = lt_fieldcat[]
       EXCEPTIONS
            OTHERS           = 0.
* remove unused fields
  LOOP AT lt_fieldcat.
    IF (
      lt_fieldcat-fieldname = 'MATNR' OR
      lt_fieldcat-fieldname = 'BWART' OR
      lt_fieldcat-fieldname = 'ERFMG' OR
      lt_fieldcat-fieldname = 'ERFMW' ).
      CLEAR lt_fieldcat-col_pos.
    ELSE.
      DELETE lt_fieldcat.
    ENDIF.
  ENDLOOP.
* display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat                       = lt_fieldcat[]
*   IT_SORT                           =
    TABLES
      t_outtab                          = lt_inttbl
    EXCEPTIONS
     OTHERS                            = 0
            .

ENDFORM.                    " bdisp

Regards,

Clemens

Read only

0 Likes
2,504

Hi suresh,

just for fun and because I never worked with the ALV SORT parameter, I added a few lines of code to get the perfect result: Quantities per material per movement type per quantity:


*&---------------------------------------------------------------------*
*&      Form  bdisp
*&---------------------------------------------------------------------*
FORM bdisp.
  DATA:
    BEGIN OF ls_inttbl,
    bwart TYPE mseg-bwart,
    matnr TYPE mseg-matnr,
    erfmg TYPE mseg-erfmg,
    erfme TYPE mseg-erfme,
    END OF ls_inttbl,
    lt_inttbl LIKE TABLE OF ls_inttbl,
    lt_fieldcat TYPE  slis_t_fieldcat_alv WITH HEADER LINE,
    lt_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE.

  lt_sort-spos = 1.
  lt_sort-fieldname = 'MATNR'.
  lt_sort-up = 'X'.
  lt_sort-subtot = 'X'.
  APPEND lt_sort.
  lt_sort-expa = 'X'.
  lt_sort-spos = 2.
  lt_sort-fieldname = 'BWART'.
  APPEND lt_sort.

* get data
  SELECT bwart matnr erfmg erfme INTO TABLE lt_inttbl
    FROM mseg
    WHERE matnr IN s_matnr.
* get field catalog with (at least) used fields
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_structure_name = 'MSEG'
       CHANGING
            ct_fieldcat      = lt_fieldcat[]
       EXCEPTIONS
            OTHERS           = 0.
* remove unused fields
  LOOP AT lt_fieldcat.
    IF (
      lt_fieldcat-fieldname = 'MATNR' OR
      lt_fieldcat-fieldname = 'BWART' OR
      lt_fieldcat-fieldname = 'ERFMG' OR
      lt_fieldcat-fieldname = 'ERFME' ).
      CLEAR lt_fieldcat-col_pos.
       lt_fieldcat-do_sum = 'X'.
       modify lt_fieldcat.
    ELSE.
      DELETE lt_fieldcat.
    ENDIF.
  ENDLOOP.
* display
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      it_fieldcat                       = lt_fieldcat[]
    IT_SORT                           = lt_sort[]
    TABLES
      t_outtab                          = lt_inttbl
    EXCEPTIONS
     OTHERS                            = 0
            .

ENDFORM.                    " bdisp

Regards,

Clemens

Read only

Former Member
0 Likes
2,504

in ur code...

while declaring internal table let the

materialcode be first field and movement type be second field.

now sort itab by materialcode movementtype.

loop at itab.

at end of movementtype.

read itab index sy-tabix. "to avoid few fields populated with *** instead of values

itab_final-materialcode = itab-materialcode.

itab_final-movementtype = itab-movementtype.

sum.

*--quanity field of all records with same materialcode and movement type...will be added up and stored in itab-quantity

itab_final-quantity_sum = itab-quantity.

append itab_final.

clear itab_final.

endat.

endloop.

use the fieldnames...as i hav used descriptions instead.....

Regards

Vasu

Read only

Former Member
0 Likes
2,504

Hi suresh,

i have a develop a small code for u.i have a develop a code similar to ur problem.i.e based on sales doc number and based on sales document item and i have to sum NETWR field in my code.please observe the code carefully which is present in form "FIELD_CATALOG" and the PARAMETER i used is "DO_SUM".

if u r satify with the answer give me REWARD POINTS.

HAVE A NICE DAY.

After analyzing the code we have to change the fields according to ur requirement ok.

CODE:

----


  • Type Pools

----


TYPE-POOLS:slis.

----


  • Tables

----


TABLES: vbak,vbap.

----


  • Global Variable

----


data: w_var type i.

----


  • Global Data

----


DATA:it_fieldcat TYPE slis_t_fieldcat_alv,

wa_fieldcat TYPE slis_fieldcat_alv,

it_sortcat TYPE slis_t_sortinfo_alv,

wa_sortcat LIKE LINE OF it_sortcat.

----


  • Internal Table

----


data: BEGIN OF it_salesorder OCCURS 0,

vbeln LIKE vbak-vbeln, " Sales Document Number

posnr like vbap-posnr, " Sales Doc Item

netwr like vbap-netwr, " Net Value

END OF it_salesorder.

----


  • SELECT OPTIONS

----


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

SELECT-OPTIONS: s_vbeln FOR vbak-vbeln. " Sales Document Number.

SELECTION-SCREEN END OF BLOCK b1.

----


  • Initialization

----


INITIALIZATION.

PERFORM initialization.

&----


*& Form initialization

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form initialization .

s_vbeln-sign = 'I'.

s_vbeln-option = 'BT'.

s_vbeln-low = '4969'.

s_vbeln-high = '5000'.

APPEND s_vbeln.

endform. " initialization

----


  • Start Of Selection

----


START-OF-SELECTION.

PERFORM field_catalog. "For Structure Creation

PERFORM fetch_data. "Get the Data From DB Table

PERFORM sorting USING it_sortcat.

----


  • End Of Selection

----


END-OF-SELECTION.

perform display_data.

&----


*& Form field_catalog

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form field_catalog .

wa_fieldcat-col_pos = w_var. " Column Position Variable

wa_fieldcat-tabname = 'IT_SALESORDER'. " Internal Table Name

wa_fieldcat-fieldname = 'VBELN'. " Field Name

wa_fieldcat-key = 'X'. " Blue Color

wa_fieldcat-ref_tabname = 'VBAK'. " Table Name

wa_fieldcat-ref_fieldname = 'VBELN'. " Field Name

wa_fieldcat-seltext_m = 'Sales Doc No'. " Display Text In Screen

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.

ADD 1 TO w_var.

wa_fieldcat-col_pos = w_var. " Column Position Variable

wa_fieldcat-tabname = 'IT_SALESORDER'. " Internal Table Name

wa_fieldcat-fieldname = 'POSNR'. " Field Name

wa_fieldcat-ref_tabname = 'VBAP'. " Table Name

wa_fieldcat-ref_fieldname = 'POSNR'. " Field Name

wa_fieldcat-seltext_m = 'Sales Doc Item'. " Display Text In Screen

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.

ADD 1 TO w_var.

wa_fieldcat-col_pos = w_var. " Column Position Variable

wa_fieldcat-tabname = 'IT_SALESORDER'. " Internal Table Name

wa_fieldcat-fieldname = 'NETWR'. " Field Name

wa_fieldcat-ref_tabname = 'VBAP'. " Table Name

wa_fieldcat-ref_fieldname = 'NETWR'. " Field Name

wa_fieldcat-do_sum = 'X'. " Sum

wa_fieldcat-seltext_m = 'Net Value'. " Display Text In Screen

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.

ADD 1 TO w_var.

endform. " field_catalog

&----


*& Form sorting

&----


  • text

----


  • -->P_IT_SORTCAT text

----


form sorting using p_it_sortcat TYPE slis_t_sortinfo_alv.

wa_sortcat-fieldname = 'VBELN'.

wa_sortcat-up ='X'.

wa_sortcat-subtot = 'X'.

APPEND wa_sortcat TO p_it_sortcat.

endform. " sorting

&----


*& Form display_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form display_data .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

  • I_INTERFACE_CHECK = ' '

  • I_BYPASSING_BUFFER = ' '

  • I_BUFFER_ACTIVE = ' '

I_CALLBACK_PROGRAM = SY-REPID

  • I_CALLBACK_PF_STATUS_SET = ' '

  • I_CALLBACK_USER_COMMAND = ' '

  • I_CALLBACK_TOP_OF_PAGE = ' '

  • I_CALLBACK_HTML_TOP_OF_PAGE = ' '

  • I_CALLBACK_HTML_END_OF_LIST = ' '

  • I_STRUCTURE_NAME =

  • I_BACKGROUND_ID = ' '

  • I_GRID_TITLE =

  • I_GRID_SETTINGS =

  • IS_LAYOUT =

IT_FIELDCAT = it_fieldcat

  • IT_EXCLUDING =

  • IT_SPECIAL_GROUPS =

IT_SORT = it_sortcat

  • IT_FILTER =

  • IS_SEL_HIDE =

  • I_DEFAULT = 'X'

  • I_SAVE = ' '

  • IS_VARIANT =

  • IT_EVENTS =

  • IT_EVENT_EXIT =

  • IS_PRINT =

  • IS_REPREP_ID =

  • I_SCREEN_START_COLUMN = 0

  • I_SCREEN_START_LINE = 0

  • I_SCREEN_END_COLUMN = 0

  • I_SCREEN_END_LINE = 0

  • IT_ALV_GRAPHICS =

  • IT_HYPERLINK =

  • IT_ADD_FIELDCAT =

  • IT_EXCEPT_QINFO =

  • I_HTML_HEIGHT_TOP =

  • I_HTML_HEIGHT_END =

  • IMPORTING

  • E_EXIT_CAUSED_BY_CALLER =

  • ES_EXIT_CAUSED_BY_USER =

TABLES

t_outtab = it_salesorder

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

endform. " display_data

&----


*& Form fetch_data

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form fetch_data .

select a~vbeln

posnr

b~netwr

from vbak as a

inner join vbap as b on avbeln = bvbeln

into table it_salesorder

where a~vbeln in s_vbeln.

endform. " fetch_data

Read only

0 Likes
2,504

Hi Kiran,

<b>

Shame Shame Shame Shame on you.</b>

Regards,

Clemens