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

how to add fields

Former Member
0 Likes
1,094

Hi All

I have a requirement,

where i have 2 fields

employee & Amount

suppose i have repeated entreis for the employess

a---- 10

a---- 10

a---- 10

a---- 10

b----5

b----5

Hence now my answer should be something lile foe field A answer is 40 rs & field B has 10 rs.

any inputs plz

Regards

Rohini

Hi All

I have a requirement,

where i have 2 fields

employee & Amount

suppose i have repeated entreis for the employess

a---- 10

a---- 10

a---- 10

a---- 10

b----5

b----5

Hence now my answer should be something lile foe field A answer is 40 rs & field B has 10 rs.

any inputs plz

Regards

Rohini

8 REPLIES 8
Read only

Former Member
0 Likes
980

Use collect statement. This will add the numeric values when the data is appended in the internal table

Read only

prasanth_kasturi
Active Contributor
0 Likes
980

hi

use collect insted of append when you are entering data nto table. and be sure that the field is of numeric data type otherwise the value cannot be added

regards

prasanth

Read only

Former Member
0 Likes
980

Hi,

As suggested collect statement will suit your requirement

declare another internal table internaltable2 of the same type containing the 2 fields internaltable1 and also a workarea

loop at internaltable1 to workarea.

collect workarea to internaltable2

endloop.

Regards

Byju

Read only

former_member480923
Active Contributor
0 Likes
980

Use the following code:



*&---------------------------------------------------------------------*
*& Report  ZTEST_PRG6
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztest_prg6.

TYPES: BEGIN OF types_data,
       fld1 TYPE char4,
       fld2 TYPE dmbtr,
       END OF types_data.

DATA: lt_data TYPE STANDARD TABLE OF types_data,
      ls_data TYPE types_data.

ls_data-fld1 = 'A'.
ls_data-fld2 = 10.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

ls_data-fld1 = 'A'.
ls_data-fld2 = 10.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

ls_data-fld1 = 'A'.
ls_data-fld2 = 10.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

ls_data-fld1 = 'A'.
ls_data-fld2 = 10.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

ls_data-fld1 = 'B'.
ls_data-fld2 = 05.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

ls_data-fld1 = 'B'.
ls_data-fld2 = 05.
COLLECT ls_data INTO lt_data.
CLEAR: ls_data.

SORT lt_data BY fld1.

LOOP AT lt_data INTO ls_data.

  WRITE:/ ls_data-fld1,
          ls_data-fld2.

ENDLOOP.

Hope That Helps

Anirban M.

Read only

vinod_vemuru2
Active Contributor
0 Likes
980

Hi Rohoni,

U can use either collect or SUM statements to achieve this.

or else following logic.

SORT itab BY employee.

LOOP AT itab INTO wa.

logic1

w_amount = w_amount + wa-amount.

AT END OF employee.

WRITE w_amount.

CLEAR w_amount.

ENDAT.

logic2

SUM.

AT END OF employee.

WRITE wa-amount.

ENDAT.

ENDLOOP.

Thanks,

Vinod.

Read only

Former Member
0 Likes
980

Its better to use COLLECT statement,but be sure that the fields that have to be added should of type I,P,F data types only.

Read only

Former Member
0 Likes
980

Its Better to use COLLECT statement,but make sure that the fields which are to be added are of data type I,P,F.

Reward if helpful

cheers

Amith

Read only

Former Member
0 Likes
980

Solved myself.