Skip to Content
author's profile photo Former Member
Former Member

To find sum in an internal table - Bit urgent

Hi SAP experts,

I have an internal table with the following fields and data :

Emp_number Section Days

1 A 10

1 B 20

3 A 20

3 B 10

2 A 20

2 B 10

Now I want to calculate the total days for each employee which is a sum of days of section A and days of section B.

I want the data as below into another internal table

:

Emp_number Total_days

1 30

2 30

3 30

Could any one tell the code for the same.

Useful answers wud be rewarded.

Vishnu.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2008 at 09:26 PM

    COLLECT the data into a separate internal table.

    Rob

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 09, 2008 at 08:14 PM

    Reply/Reward/close the thread if your issue is resolved.

    Raja T

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jan 08, 2008 at 09:26 PM

    Here you go:

    <div class="jive-quote">

    BEGIN OF IT_SUM

    selction TYPE C,

    Days TYPE I, " << should be I, P or F to get sum for that field

    END OF IT_SUM.

    LOOP AT ITAB.

    MOVE-CORESSPONDING ITAB TO IT_SUM.

    COLLECT IT_SUM.

    CLEAR IT_SUM.

    ENDLOOP.

    </div>

    Raja T

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2008 at 09:29 PM

    hi,

    emp_tab1 is Emp_number Section Days .

    emp_tab2 is Emp_number Total_days.

    field-symbols <fs_emp> like emp_tab2.

    loop at emp_tab1.

    read table emp_tab2 assigning <fs_emp> where Emp_number = emp_tab1-Emp_number.

    if sy-subrc = 0.

    add emp_tab1-Days to <fs_emp>-total_days.

    else.

    move emp_tab1-Emp_number to emp_tab2-Emp_number.

    move emp_tab1-days to emp_tab2-total_days.

    append emp_tab2.

    clear emp_tab2.

    endif.

    endloop.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 08, 2008 at 09:55 PM
    DATA : begin of itab2 occurs 0,  "new internal table
     emp_number like itab-emp_number,
    days like itab-days,
    end of itab2.
    
    "your code goes here.
    
    loop at itab.  
    itab2-emp_number = itab-emp_number.
    itab2-days = itab-days.
    COLLECT itab2.
    endloop.
    
    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jan 10, 2008 at 10:57 AM

    Hi Vishwanath,

    In this case you have to use CONTROL BREAK STATEMENTS.

    1.At first

    2.At new

    3.At end

    4.At last

    1.At first is going to trigger at the 1st loop of the internal table.From the 2nd loop it is not going to trigger.

    It is mainly used for sub headings.

    2.At new is going to trigger at the new value of the internal table.

    3. At end is going to trigger at the end of the new value of a particular field.

    It is mainly use for subtotals.

    4.At last is going to trigger at the last loop of the internal table.

    It is mainly use for grand totals.

    So you gothrough the above 4 statements.

    In your case you have to use the third statement i.e., At end.

    At end

    -


    ur logic

    -


    endat.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.