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: 

How to sum entries in a internal table

0 Kudos

Hi Experts,

Below is the data which is there in my internal table.

BUKRS BELNR GJAHR TXGRPHWSTE
10005100000135201414000
10005100000135201413000
10005100000135201428000
10005100000135201426000
100051000001352014312000
10005100000135201439000

My requirement is to sum the data when the TXGRP field is repeating and the internal table should store only one entry if multiple entries found against a unique combination along with the sum of the field HWSTE.

Below is the to be data.

BUKRS BELNR GJAHR TXGRPHWSTE
10005100000135201417000
100051000001352014214000
100051000001352014321000

Please guide me to achieve this.

Thanks & Regards,

Naveen.

1 ACCEPTED SOLUTION

Former Member

HI Nishuv,

How Collect works :

1. First it will check in internal table for any record matching with the key in work area data.

2. If it couldn’t find any matching record, then the new data from work area will be inserted in internal     table

3. If any record found with the same key, then instead of inserting a new record, it will add the          numeric  field values of work area components with the corresponding field components in the    matched record and update the internal table record.

So Collect should work fine for your code ,just use it in  as mentioned below :


1. Declare structure using BUKRS , BELNR, GJAHR ,TXGRP ,HWSTE fields  .

2. Now use ,Select query to fetch these fields into an internal table <int_tab>.

3. Use loop statement

    loop at <int_tab> into <wa>

    collect <wa> into <int_tab>

    endloop.

Now,u can see that<int_tab> will have those entries summarized based on unique combination

I Hope it will prove useful .

Best Regards,

Praveen Srivastava .

5 REPLIES 5

FredericGirod
Active Contributor
0 Kudos

Hi,

have a look to the statement COLLECT

regards

Fred

Former Member
0 Kudos

hi Naveen,

refer blow code:

   TYPES: BEGIN OF ty_itab,
  bukrs TYPE bukrs,
  belnr TYPE belnr,
  gjahr TYPE gjahr,
  txgrp TYPE txgrp,
  hwste TYPE hwste,
  END OF ty_itab.

DATA: itab TYPE STANDARD TABLE OF ty_itab WITH HEADER LINE,
      itab2 TYPE STANDARD TABLE OF ty_itab WITH HEADER LINE,
      wa_itab LIKE LINE OF itab,
      hwste_sum TYPE hwste.

START-OF-SELECTION.
  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '1'.
  itab-hwste = '4000'.
  APPEND itab.

  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '2'.
  itab-hwste = '6000'.
  APPEND itab.

  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '1'.
  itab-hwste = '3000'.
  APPEND itab.

  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '3'.
  itab-hwste = '9000'.
  APPEND itab.

  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '2'.
  itab-hwste = '8000'.
  APPEND itab.



  itab-bukrs = '1000'.
  itab-belnr = '5100000135'.
  itab-gjahr = '2014'.
  itab-txgrp = '3'.
  itab-hwste = '12000'.
  APPEND itab.



  SORT itab BY bukrs belnr gjahr txgrp.

  LOOP AT itab .
    MOVE-CORRESPONDING itab TO wa_itab.
    hwste_sum = hwste_sum + itab-hwste.
    AT END OF txgrp.
      wa_itab-hwste = hwste_sum.
      APPEND wa_itab TO itab2.
      CLEAR hwste_sum.
    ENDAT.
  ENDLOOP.

  LOOP AT itab2.
    WRITE: /,itab2-bukrs,itab2-belnr,itab2-gjahr,itab2-txgrp,itab2-hwste.
  ENDLOOP.

regards,

Archer

Former Member
0 Kudos

Hi,

Simply you can use the select statement ! It should work!

Since BSEG-TXGRP is character field! so it will some based on your requirement

check like this

LOOP AT YOUR INTERNAL TABLE.

COLLECT ITAB TO ITAB1

ENDLOOP.

check results in itab1

thangam_perumal
Contributor
0 Kudos

Hi Nishuv,

                  You can Achieve your requirements with help of your Collect Statements.

Please Refer the Below Code.

Consider your existing Internal table and work area as ITAB, WA.

data: begin of wa1,

bukrs(4) type  c,

belnr(10) type c,

gjahr(4)  type c,

txgrp(4)  type c,

hwste(10) type  n (or) P (or) i,

end of wa1.

data : itab1 like wa1 occurs 0.

Loop at itab into wa,

wa1-bukrs = wa-bukrs .

wa1-belnr  = wa-belnr .

wa1-gjahr  = wa-gjahr .

wa1-txgrp =  wa-txgrp.

wa1-hwste =wa-hwste.

collect wa1 into itab1.

endloop.

i think it will be enough for your requirements  , if you have any queries  post here or please study about collect statements.

Regards,

Thangam.P

Former Member

HI Nishuv,

How Collect works :

1. First it will check in internal table for any record matching with the key in work area data.

2. If it couldn’t find any matching record, then the new data from work area will be inserted in internal     table

3. If any record found with the same key, then instead of inserting a new record, it will add the          numeric  field values of work area components with the corresponding field components in the    matched record and update the internal table record.

So Collect should work fine for your code ,just use it in  as mentioned below :


1. Declare structure using BUKRS , BELNR, GJAHR ,TXGRP ,HWSTE fields  .

2. Now use ,Select query to fetch these fields into an internal table <int_tab>.

3. Use loop statement

    loop at <int_tab> into <wa>

    collect <wa> into <int_tab>

    endloop.

Now,u can see that<int_tab> will have those entries summarized based on unique combination

I Hope it will prove useful .

Best Regards,

Praveen Srivastava .