06-27-2007 11:43 AM
Hi Folks,
I have two fields hkont and wrbtr.
at every new hkont i want to sum up wrbtr.
I don't want to use write statement in the code.
loop at ittcs1.
at new ittcs1-hkont.( Or on change of ?)
sum ittcs1-wrbtr.
endat.
K.Kiran.
06-27-2007 11:47 AM
HI.
refer this .
SUM
Basic form
SUM.
Effect
When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).
You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST ).
Example
Display the table T with sub-totals:
DATA: BEGIN OF T OCCURS 100,
CODE(4),
SALES TYPE P,
DISCOUNT TYPE P,
END OF T.
...
LOOP AT T.
AT FIRST.
SUM.
WRITE: /4 'Grand Total:',
20 T-SALES, 40 T-DISCOUNT.
ULINE. SKIP.
ENDAT.
WRITE: / T-CODE,
20 T-SALES, 40 T-DISCOUNT.
AT END OF CODE.
SUM.
WRITE: / T-CODE, 10 'Total:',
20 T-SALES, 40 T-DISCOUNT.
SKIP.
ENDAT.
ENDLOOP.
Notes
When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.
When using LOOP to process a sorted extract (see SORT ), the control total of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .
Note
Runtime errors
SUM_OVERFLOW : Value too large when calculatng totals in internal table, field too small.
SUM_NO_INTERNAL_TABLE : The SUM statement was used outside a LOOP on an internal table.
SUM_ON_FOREIGN_INTERNAL_TABLE : The SUM statement was used in a LOOP belonging to another ABAP/4 program.
COLLECT
Basic form
COLLECT [wa INTO] itab.
Addition
... SORTED BY f
Effect
COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .
If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.
If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.
If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.
If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .
After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.
Notes
COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.
If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that
the internal table will actually be unique or compressed, as described above and
COLLECT will run very efficiently.
If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.
Example
Compressed sales figures for each company
DATA: BEGIN OF COMPANIES OCCURS 10,
NAME(20),
SALES TYPE I,
END OF COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.
COLLECT COMPANIES.
COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.
COLLECT COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.
COLLECT COMPANIES.
The table COMPANIES now has the following appearance:
NAME SALES
Duck 40
Tiger 20
Addition
... SORTED BY f
Effect
COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.
Note
Performance
The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.
If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.
A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).
Note
Runtime errors
COLLECT_OVERFLOW : Overflow in integer field when calculating totals.
COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.
Related APPEND , WRITE ... TO , MODIFY , INSERT
Reward all helpfull answers.
Regards.
Jay
06-27-2007 11:47 AM
HI.
refer this .
SUM
Basic form
SUM.
Effect
When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).
You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST ).
Example
Display the table T with sub-totals:
DATA: BEGIN OF T OCCURS 100,
CODE(4),
SALES TYPE P,
DISCOUNT TYPE P,
END OF T.
...
LOOP AT T.
AT FIRST.
SUM.
WRITE: /4 'Grand Total:',
20 T-SALES, 40 T-DISCOUNT.
ULINE. SKIP.
ENDAT.
WRITE: / T-CODE,
20 T-SALES, 40 T-DISCOUNT.
AT END OF CODE.
SUM.
WRITE: / T-CODE, 10 'Total:',
20 T-SALES, 40 T-DISCOUNT.
SKIP.
ENDAT.
ENDLOOP.
Notes
When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.
When using LOOP to process a sorted extract (see SORT ), the control total of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .
Note
Runtime errors
SUM_OVERFLOW : Value too large when calculatng totals in internal table, field too small.
SUM_NO_INTERNAL_TABLE : The SUM statement was used outside a LOOP on an internal table.
SUM_ON_FOREIGN_INTERNAL_TABLE : The SUM statement was used in a LOOP belonging to another ABAP/4 program.
COLLECT
Basic form
COLLECT [wa INTO] itab.
Addition
... SORTED BY f
Effect
COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .
If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.
If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.
If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.
If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .
After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.
Notes
COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.
If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that
the internal table will actually be unique or compressed, as described above and
COLLECT will run very efficiently.
If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.
Example
Compressed sales figures for each company
DATA: BEGIN OF COMPANIES OCCURS 10,
NAME(20),
SALES TYPE I,
END OF COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.
COLLECT COMPANIES.
COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.
COLLECT COMPANIES.
COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.
COLLECT COMPANIES.
The table COMPANIES now has the following appearance:
NAME SALES
Duck 40
Tiger 20
Addition
... SORTED BY f
Effect
COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.
Note
Performance
The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.
If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.
A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).
Note
Runtime errors
COLLECT_OVERFLOW : Overflow in integer field when calculating totals.
COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.
Related APPEND , WRITE ... TO , MODIFY , INSERT
Reward all helpfull answers.
Regards.
Jay
06-27-2007 11:48 AM
Hi Kiran,
when you fill the itab try collect itab not appen itab.
Regards, Dieter
06-27-2007 11:49 AM
If you use sum, then you should use write statement to display the subtotals or total. Otherwise use ALV report.
06-27-2007 11:53 AM
06-27-2007 12:08 PM
If your internal table only contains two fields HKONT and WRBTR then you can use AT END OF control break statement:
sort ittcs1 by hkont.
loop at ittcs1.
at end of hkont.
sum ittcs1-wrbtr.
endat.
Even if your table contains more than two fields you can use the above so long as there are no fields to the left of HKONT that will affect the control break.
If you have many fields in the table and HKONT is not the left most field then you can use COLLECT.
Data: I_collect (declared as an internal table with only two fields HKONT and WRBTR)
loop at ittcs1.
l_wa_collect-hkont = ittcs1-hkont.
l_wa_collect-wrbtr = ittcs1-wrbtr.
collect I_wa_collect into I_collect.
endat.
Hope it helps
06-27-2007 12:15 PM
Hi,
Try like this:
loop at ittcs1.
at new hkont.
at end of wrbtr.
sum.
enat.
endat.
endloop.
Regards,
Bhaskar
07-24-2007 6:31 AM