2007 Jan 24 8:49 AM
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.
2007 Jan 24 8:54 AM
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
2007 Jan 24 8:55 AM
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
2007 Jan 24 8:55 AM
2007 Jan 24 8:56 AM
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.
2007 Jan 24 8:56 AM
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