2006 Feb 10 10:48 AM
Hi all,.
I need to load some data into an internal table.
I have to sum a field and select it.
Presently, I am using the select statement followed by group by clause.
Like ,
select field1
sum( field2 )
from ....
group by
Is there any other way to do it.
Regards,
Priya
Hi all,.
I need to load some data into an internal table.
I have to sum a field and select it.
Presently, I am using the select statement followed by group by clause.
Like ,
select field1
sum( field2 )
from ....
group by
Is there any other way to do it.
Regards,
Priya
2006 Feb 10 10:51 AM
Hi priya,
Yes we can do it using ALV's in that use sum option.and one more thing don't use these arthemetic operations and group by b'coz it consumes the systenm resources.
regards,
<i><b>Sri</b></i>
2006 Feb 10 10:52 AM
2006 Feb 10 10:52 AM
hi,
the way you are doing is correct.
you can also do at itab level by using SUM statement.
Regards,
Abdul Hakim
2006 Feb 10 10:53 AM
HI Priya,
You can better bring the contents to the internal table and
LOOP AT ITAB.
....
AT LAST.
SUM.
ENDAT.
ENDLOOP.
I think this is better ..
regards
satesh
2006 Feb 10 10:54 AM
Hi Priya,
First do normal selection of data using Select Statement.
Sort the Internal Table.
Then use COLLECT statement.
Here the SAP help For u .
The following statement allows you to summate entries in an internal table:
COLLECT <wa> INTO <itab>.
<itab> must have a flat line type, and all of the fields that are not part of the table key must have a numeric type (F, I, or P). You specify the line that you want to add in a work area that is compatible with the line type.
When the line is inserted, the system checks whether there is already a table entry that matches the key. If there is no corresponding entry already in the table, the COLLECT statement has the same effect as inserting the new line. If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.
You should only use the COLLECT statement if you want to create summarized tables. If you use other statements to insert table entries, you may end up with duplicate entries.
Lines are added to internal tables as follows:
Standard tables
If the COLLECT statement is the first statement to fill the standard table, the system creates a temporary hash administration that identifies existing entries in the table. The hash administration is retained until another statement changes the contents of key fields or changes the sequence of the lines in the internal table. After this, the system finds existing entries using a linear search. The runtime for this operation increases in linear relation to the number of existing table entries. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.
Sorted tables
The system uses a binary search to locate existing lines. The runtime for the operation increases logarithmically with the number of existing lines. The system field SY-TABIX contains the index of the line inserted or modified in the COLLECT statement.
Hashed tables
The system finds existing lines using the hash algorithm of the internal table. After the COLLECT statement, the system field SY-TABIX has the value 0, since hashed tables have no linear index.
Example
DATA: BEGIN OF LINE,
COL1(3) TYPE C,
COL2(2) TYPE N,
COL3 TYPE I,
END OF LINE.
DATA ITAB LIKE SORTED TABLE OF LINE
WITH NON-UNIQUE KEY COL1 COL2.
LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 3.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LINE-COL1 = 'def'. LINE-COL2 = '34'. LINE-COL3 = 5.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LINE-COL1 = 'abc'. LINE-COL2 = '12'. LINE-COL3 = 7.
COLLECT LINE INTO ITAB.
WRITE / SY-TABIX.
LOOP AT ITAB INTO LINE.
WRITE: / LINE-COL1, LINE-COL2, LINE-COL3.
ENDLOOP.
The output is:
1
2
1
abc 12 10
def 34 5
The example fills a sorted table. The first two COLLECT statements work like normal insertion statements. In the third COLLECT statement, the first line of ITAB is modified. The following diagram shows the three steps:
Thanks,
Srinivas.
2006 Feb 10 10:56 AM
HI priya
If you want to print the sum in your report then you have to use
Loop at itab.
at last.
sum.
write: itab-<fieldname>.
endat.
endloop.
this will print the sum.
But if you wnat the sum to be in a variable then you have to use
select field1 sum(field2) into <itab> from <table> group by <key field>.
regards
kishore
2006 Feb 10 10:59 AM
HI priya,
1. Is there any other way to do it.
If u want only the sum, (not the detail info)
, then this is the best way.
(ie., instead of reading all records from database
,and then applying sum logic,
we can STRAIGHT away read the sum
using the SQL)
2. However, if u ALSO want to show
the detailed records,
then u need to fetch all records first,
and then either use
a) collect
b) use alv total functionality
c) something else
regards,
amit m.
2006 Feb 10 11:00 AM
Hi,
i feel select the records, later using control break statements or collect you can sum them , performance point it is better opttion.
regards
vijay