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

internal table

Former Member
0 Likes
910

my internal table contains following values

fild1 field2

ea 5

ea 5

ea 5

sp 5

sp 5

sp 6

i want to add all ea or sp values and display in script how to do it

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
881

You can use collect statement to get the sum.

See the example to collect statement:

TYPES: BEGIN OF COMPANY, 
        NAME(20) TYPE C, 
        SALES    TYPE I, 
      END OF COMPANY. 

DATA: COMP    TYPE COMPANY, 
      COMPTAB TYPE HASHED TABLE OF COMPANY 
                                WITH UNIQUE KEY NAME. 

COMP-NAME = 'Duck'.  COMP-SALES = 10. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Duck'.  COMP-SALES = 30. COLLECT COMP INTO COMPTAB. 



Table COMPTAB now has the following contents: 

          NAME    | SALES 
          --------------- 
          Duck    |   40 
          Tiger   |   20

Now you can apss it to sapscript.

9 REPLIES 9
Read only

Former Member
0 Likes
882

You can use collect statement to get the sum.

See the example to collect statement:

TYPES: BEGIN OF COMPANY, 
        NAME(20) TYPE C, 
        SALES    TYPE I, 
      END OF COMPANY. 

DATA: COMP    TYPE COMPANY, 
      COMPTAB TYPE HASHED TABLE OF COMPANY 
                                WITH UNIQUE KEY NAME. 

COMP-NAME = 'Duck'.  COMP-SALES = 10. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB. 
COMP-NAME = 'Duck'.  COMP-SALES = 30. COLLECT COMP INTO COMPTAB. 



Table COMPTAB now has the following contents: 

          NAME    | SALES 
          --------------- 
          Duck    |   40 
          Tiger   |   20

Now you can apss it to sapscript.

Read only

Former Member
0 Likes
881

hi

loop.

at end of fild1.

sum.

write : fld1.

endat.

endloop.

Read only

Former Member
0 Likes
881

You can use sum function...

and then according to you.

at end of fild1.

sum.

write:/ 'Sum',

intab-fild1 UNDER intab-fild1.

SKIP.

ENDAT.

  • This is for general report.

In the script write the same fieldname.... you will get the desired output...

Regards,

Jayant

Read only

Former Member
0 Likes
881

Hi Shankar ,

Write a Sub Routine and do Perform sub_routine

Endperform

inside the script and try to use itcsy

data:begin of itab occurs 0,

fild1 type char5 ,

field2 type char5,

end of itab.

u add the values to this internal table and create an element use in the

WRITE FORM PROVIDE LOOP ENDLOOP between WriteForm.

and provide control breaks inside the Loop where required .

Form Sub_rotine tables i_input structure itcsy

o_output structure itcsy.

CALL FUNCTION 'START_FORM'

  • EXPORTING

  • ARCHIVE_INDEX =

  • FORM = ' '

  • LANGUAGE = ' '

  • STARTPAGE = ' '

  • PROGRAM = ' '

  • MAIL_APPL_OBJECT =

  • IMPORTING

  • LANGUAGE =

  • EXCEPTIONS

  • FORM = 1

  • FORMAT = 2

  • UNENDED = 3

  • UNOPENED = 4

  • UNUSED = 5

  • SPOOL_ERROR = 6

  • CODEPAGE = 7

  • OTHERS = 8

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

LOOP at itab.

CALL FUNCTION 'WRITE_FORM'

  • EXPORTING

  • ELEMENT = ' '

  • FUNCTION = 'SET'

  • TYPE = 'BODY'

  • WINDOW = 'MAIN'

  • IMPORTING

  • PENDING_LINES =

  • EXCEPTIONS

  • ELEMENT = 1

  • FUNCTION = 2

  • TYPE = 3

  • UNOPENED = 4

  • UNSTARTED = 5

  • WINDOW = 6

  • BAD_PAGEFORMAT_FOR_PRINT = 7

  • SPOOL_ERROR = 8

  • CODEPAGE = 9

  • OTHERS = 10

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

endloop

END_FORM

Endform.

Read only

Former Member
0 Likes
881

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.

Read only

Former Member
0 Likes
881

generally collect statement is used to sum up all the numeric fields into the 1st field and it will display as a single unit.

when collect used for for other than numeric fileds then it displays the top most record od an internal table thus by eleminating the duplicates

Read only

Former Member
0 Likes
881

data : itab2 like itab1 occurs 0 with header line.

loop at itab1.

read table itab2 with key fld1 = itab1-fld1.

if sy-subrc = 0.

itab2-fld2 = itab1-fld2 + itab2-fld2.

modify itab2 index sy-tabix.

else.

move-corresponding itab1 to itab2.

append itab2.

endif.

clear : itab1,itab2.

endloop.

loop at itab2.

now call write_form fm.

endloop.

regards

shiba dutta

Message was edited by:

SHIBA DUTTA

Read only

Former Member
0 Likes
881

We can use collect state ment without using append stmt,

Because collect statement can group the number of entries we entering...

Second one We can use our ABAP code by checking the condition (Before tht we want to sort the entire records)..But here we want break our head...

If u r very good in Programming we need not worry abt the functions and keywords.

Read only

Former Member
0 Likes
881

Hi Shankar ,

Use <b>SUM</b> command to add field values.

Loop at itab.

at end of f1.

sum.

endat.

endloop.

Reward points if helpful.

Regards,

Hemant