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

urgent

Former Member
0 Likes
827

Hi,

I wanted to do subtotal for some fields based on some condition.

I have already data in my internal table itab.

could anybody suggest me the logic for it.

the condition is like

loop at itab.

if itab-f1 = 'X'.

*now here i wanted to calculate the subtotal of particular column suppose fields are f5, f9(both r fields).

endif.

endloop.

Pls suggest me asap.

Hi,

I wanted to do subtotal for some fields based on some condition.

I have already data in my internal table itab.

could anybody suggest me the logic for it.

the condition is like

loop at itab.

if itab-f1 = 'X'.

*now here i wanted to calculate the subtotal of particular column suppose fields are f5, f9(both r fields).

endif.

endloop.

Pls suggest me asap.

5 REPLIES 5
Read only

Former Member
0 Likes
788

Hi

U can use collect statament to calculate subtotal:

DATA: BEGIN OF ITAB OCCURS 0,
              FIELD1,
              FIELD2,
              FIELD3,
              NUM1 TYPE I,
              NUM2 TYPE I,
            END   OF ITAB.

DATA: BEGIN OF TOTAL OCCURS 0,
               FIELD1,
               NUM1 TYPE I,
               NUM2 TYPE I,
           END    OF TOTAL. 

LOOP AT ITAB.
   MOVE-CORRESPONDING ITAB TO TOTAL.
   COLLECT TOTAL.
ENDLOOP.

In TOTAL it has the subtotals for field FIELD1.

If you need to know the subtotal when FIELD1 = 'X':

READ TABLE TOTAL WITH KEY FIELD1 = 'X'.

Max

Read only

Former Member
0 Likes
788

data: subtotal1 type p length 8 decimals 2,

subtotal2 type p length 8 decimals 2.

loop at itab into wa.

if wa-f1 = 'X'.

subtotal1 = subtotal1 + wa-f5.

subtotal2 = subtotal2 + wa-f9.

endloop.

write: subtotal1, subtotal2.

Message was edited by:

Florian Kemmer

Read only

Former Member
0 Likes
788

Message edited

Read only

Former Member
0 Likes
788

Use this code:

sort itab by f1.

 loop at itab into wa.
 write: wa-f1, wa-f2, waf2...

 on change of f1.
  sum.
  write: wa-f5, wa-f9. "Summed fields.

 endloop.

Read only

Former Member
0 Likes
788

Hi Neha,

first sort the internal table by f5 and f9.

loop at itab.

at end of f9.

sum.

move this result to any work area and append it to a new itab.

endat.

endloop.

reward if helpful.

Chetan