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, per material?

Former Member
0 Likes
3,510

Hi,

I have an internal table:

material price

111111 10,00

111111 15,00

222111 12,00

222111 18,00

How to use SUM to get total price PER material? Which statement tu use?

ON CHANGE of material doesnt work..I get total price of all materials..

111111 25,00

222111 30,00

thanks..

1 ACCEPTED SOLUTION
Read only

Sandeep_Panghal
Product and Topic Expert
Product and Topic Expert
0 Likes
3,132

either use at new or on change of.

26 REPLIES 26
Read only

former_member188829
Active Contributor
0 Likes
3,132

Use AT NEW matnr

ENDAT. Statements

Read only

Sandeep_Panghal
Product and Topic Expert
Product and Topic Expert
0 Likes
3,133

either use at new or on change of.

Read only

Former Member
0 Likes
3,132

U can also use at end statement to get the sum per material.

Read only

0 Likes
3,132

Hi,

Use AT NEW.

ON CHNAGE of is obsolute in ECC.

Regards,

Surendar Reddy.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,132

check this

loop at itab.

at new matnr.

sum.

write itab-price.

skip1.

endat.

Read only

Former Member
0 Likes
3,132

Hi,

You can also use LOOP AT with COLLECT statement.

Regards

Karthik D

Read only

Former Member
0 Likes
3,132

Hi,

Thanks all!

AT NEW won't work if MATERIAL isn't the first kolumn??

pos material price

Is there other solution?

tnx!

Read only

0 Likes
3,132

u can make it in the first column and try if your not using any control break statement else where.

or.

loop at itab.

it1-matnr = itab-matnr.

it1-price = itab-price.

collect it1.

endloop.

Read only

0 Likes
3,132

Did you sort the internal table before using Control break statement?

Sort itab by matnr. -


> Add this statement

Loop at itab.

At new matnr.

Sum.

Endat.

Endloop.

Read only

0 Likes
3,132

Yes I have.. but no result..

Read only

0 Likes
3,132

AT NEW won't work if MATERIAL isn't the first kolumn??

Have you tried using at new itab-matnr.

Read only

0 Likes
3,132

Yes, getting error: no component exists with the name itab-matnr

Read only

0 Likes
3,132

Example:


REPORT  zz_test_aa1.


TYPES: BEGIN OF ls_itab,
  p(3),
  matnr TYPE matnr,
  price TYPE kbetr,
  END OF ls_itab.

DATA: itab TYPE STANDARD TABLE OF ls_itab,
      wtab TYPE ls_itab.

wtab-p     = '10'.
wtab-matnr = '1111111111'.
wtab-price = '10.00'.
APPEND wtab TO itab.

wtab-p     = '20'.
wtab-matnr = '1111111111'.
wtab-price = '12.00'.
APPEND wtab TO itab.

wtab-p     = '30'.
wtab-matnr = '2211111111'.
wtab-price = '13.00'.
APPEND wtab TO itab.

wtab-p     = '40'.
wtab-matnr = '2211111111'.
wtab-price = '14.00'.
APPEND wtab TO itab.


SORT itab by matnr.
LOOP AT itab INTO wtab.

  ON CHANGE OF wtab-matnr.
    SUM.
    WRITE: wtab-matnr, wtab-price.
    SKIP 1.
  ENDON.


ENDLOOP.

Read only

0 Likes
3,132

Try this.

DATA:BEGIN OF itab OCCURS 0,
     matnr TYPE matnr,
     price TYPE wrbtr,
     END OF itab.

START-OF-SELECTION.
  CLEAR itab.
  itab-matnr = '111111'.
  itab-price = '10'.
  APPEND itab.

  CLEAR itab.
  itab-matnr = '111111'.
  itab-price = '15'.
  APPEND itab.

  CLEAR itab.
  itab-matnr = '222111'.
  itab-price = '12'.
  APPEND itab.

  CLEAR itab.
  itab-matnr = '222111'.
  itab-price = '18'.
  APPEND itab.

  SORT itab BY matnr.

  LOOP AT itab.
    AT NEW matnr.
      SUM.
      WRITE:/ itab-matnr,itab-price.
    ENDAT.
  ENDLOOP.

Read only

0 Likes
3,132

@Vishnu,

I can't change the kolumns..

Read only

0 Likes
3,132

Info to all:

I'm printing invoice sapscript so i'm using internal table tvbdpr type vbdpr so I can't change kolumns here.

Read only

0 Likes
3,132
TYPES: BEGIN OF ls_itab,
  matnr TYPE matnr,
  price TYPE kbetr,
  p(3),                        "Change it to third column from first column
  END OF ls_itab.

DATA: itab TYPE STANDARD TABLE OF ls_itab,
      wtab TYPE ls_itab.

wtab-p     = '10'.
wtab-matnr = '1111111111'.
wtab-price = '10.00'.
APPEND wtab TO itab.

wtab-p     = '20'.
wtab-matnr = '1111111111'.
wtab-price = '12.00'.
APPEND wtab TO itab.

wtab-p     = '30'.
wtab-matnr = '2211111111'.
wtab-price = '13.00'.
APPEND wtab TO itab.

wtab-p     = '40'.
wtab-matnr = '2211111111'.
wtab-price = '14.00'.
APPEND wtab TO itab.


SORT itab BY matnr.
LOOP AT itab INTO wtab.
  AT NEW matnr.                   "Change it it At new
    SUM.
    WRITE: wtab-matnr, wtab-price.
    SKIP 1.
  ENDAT.
ENDLOOP.

If we use At new material, if the left side columns change before material the control break statement wil be triggered.

Read only

0 Likes
3,132

i changed column position in my previous post. Okay i will try it with out changing the column position.

Read only

0 Likes
3,132

Yes please..:)

Read only

0 Likes
3,132

Hi,

Please try this...


TYPES: BEGIN OF ls_itab,
  p(3),
  matnr TYPE matnr,
  price TYPE kbetr,
  END OF ls_itab.

DATA: itab TYPE STANDARD TABLE OF ls_itab,
      wtab TYPE ls_itab,
      total_price TYPE kbetr ,
      matnr1 TYPE matnr,
      matnr2 TYPE matnr.

wtab-p     = '10'.
wtab-matnr = '1111111111'.
wtab-price = '10.00'.
APPEND wtab TO itab.

wtab-p     = '20'.
wtab-matnr = '1111111111'.
wtab-price = '12.00'.
APPEND wtab TO itab.

wtab-p     = '30'.
wtab-matnr = '2211111111'.
wtab-price = '13.00'.
APPEND wtab TO itab.

wtab-p     = '40'.
wtab-matnr = '2211111111'.
wtab-price = '14.00'.
APPEND wtab TO itab.

wtab-p     = '50'.
wtab-matnr = '4411111111'.
wtab-price = '16.00'.
APPEND wtab TO itab.


SORT itab BY matnr.
LOOP AT itab INTO wtab.

  matnr2 = wtab-matnr.

  IF NOT matnr1 IS INITIAL.
    IF matnr1 = matnr2.
      total_price = total_price + wtab-price.
    ELSE.
      WRITE: matnr1, total_price.
      CLEAR total_price.
      total_price = wtab-price.
    ENDIF.
  ENDIF.

  SKIP 1.

  matnr1 = matnr2.

  AT LAST.
    WRITE: matnr2, total_price.
  ENDAT.

ENDLOOP.

Read only

0 Likes
3,132

Hi,

as per your code: can you check with the below code..

SORT itab by matnr.

LOOP AT itab INTO wtab.

v_price = v_price+ wtab-price.

At end of Matnr

WRITE: wtab-matnr, v_price.

clear v_price.

endat.

ENDLOOP.

Edited by: chaitanya on Jun 15, 2009 3:17 PM

Read only

Former Member
0 Likes
3,132

Use Collect Statement,

This is the most suited alternative .

Read only

Nawanandana
Active Contributor
0 Likes
3,132

hi

First thing is u have to change ur internal table starting with matnr

ex: DATA: beging of' itab,
                matnr like mseg matnr,
                xxxxx like mseg-xxxx,
                ..............................,

                end of itab.
{CODE}

thn you have to sort it by MATNR(material number)

now within the loop u can used
 

AT END OF....... ENDAT

cheers

nawa

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,132

if its not in the first column u cannot use control break events.

if its sap script try to create a routine and use collect statement.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,132

you have nearly 242 posts, still stuck with these sort of questions

all releavent answers are been provided .. now apply the right one and close the thread.

Such a long discussion

Read only

former_member188829
Active Contributor
0 Likes
3,132

Try this.

I don't know wether it is suitable logic or not.

TYPES: BEGIN OF ls_itab,
  p(3),
  matnr TYPE matnr,
  price TYPE kbetr,
  END OF ls_itab.

DATA: itab TYPE STANDARD TABLE OF ls_itab,
      wtab TYPE ls_itab.

DATA: BEGIN OF ls_itab1 OCCURS 0,
  matnr TYPE matnr,
  price TYPE kbetr,
  END OF ls_itab1.

DATA:v_sum TYPE kbetr.

wtab-p     = '10'.
wtab-matnr = '1111111111'.
wtab-price = '10.00'.
APPEND wtab TO itab.
CLEAR wtab.

wtab-p     = '20'.
wtab-matnr = '1111111111'.
wtab-price = '12.00'.
APPEND wtab TO itab.
CLEAR wtab.

wtab-p     = '30'.
wtab-matnr = '2211111111'.
wtab-price = '13.00'.
APPEND wtab TO itab.
CLEAR wtab.

wtab-p     = '40'.
wtab-matnr = '2211111111'.
wtab-price = '14.00'.
APPEND wtab TO itab.
CLEAR wtab.

LOOP AT itab INTO wtab.
  MOVE-CORRESPONDING wtab TO ls_itab1.
  APPEND ls_itab1.
ENDLOOP.
SORT ls_itab1 BY matnr.
SORT itab BY matnr.
LOOP AT itab INTO wtab.
  ON CHANGE OF wtab-matnr.
    LOOP AT ls_itab1 WHERE matnr = wtab-matnr.
      AT NEW matnr.
        SUM.
        v_sum = ls_itab1-price.
      ENDAT.
    ENDLOOP.
    WRITE: wtab-matnr, v_sum.
    CLEAR v_sum.
  ENDON.
  SKIP 1.
ENDLOOP.