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 insert sub-total inside the internal table data.

Former Member
0 Likes
1,395

HI all ...

I am little confused with this below issue...

I need to insert the sub-total inside the internal table.

For ex: in the below code

there are 3 data Personnel number , Employee gorup , Total.

So output of this data

ex:

perrn empgrp total

1 1 1

2 1 1

3 2 1

4 3 1

5 3 1

6 4 1

7 4 1

Reuirement is to display the total for empgroup 1-2 , 3 , 4.

so now we want to loop the data.

perrn empgrp total

1 1 1

2 1 1

3 2 1

  • * 3-> Addition of Emp gorup 1 & 2

4 3 1

5 3 1

  • * 2->Addition of Emp Group 3

6 4 1

7 4 1

  • * 2 -> Addition of emp Group 4.

How to add based on 2 emp gorups and how to insert in between the internal table ...Please any advice?

Just have a condition that should not use another internal table.

and Please conside that * as space .


For ex: 
REPORT YTEST_XLS_COL LINE-SIZE 300.
TABLES: PERNR.
INFOTYPES: 0001.
DATA: BEGIN OF ITAB OCCURS 1,
PERNR TYPE PERNR,
EG TYPE P0001-PERSG,
TOTAL TYPE I,
END OF ITAB.

START-OF-SELECTION.
GET PERNR.
RP_PROVIDE_FROM_LAST P0001 SPACE PN-BEGDA PN-ENDDA.
MOVE PERNR-PERNR TO ITAB-PERNR.
MOVE P0001-PERSG TO ITAB-EG.
CLEAR ITAB-TOTAL.
ITAB-TOTAL = ITAB-TOTAL + 1.
APPEND ITAB.

END-OF-SELECTION.
SORT ITAB BY EG .
LOOP AT ITAB.
WRITE: / ITAB-PERNR ,20 ITAB-EG ,25 ITAB-TOTAL.
ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,365

Hi,

try it now.

REPORT YTEST_XLS_COL LINE-SIZE 300.

TABLES: PERNR.
INFOTYPES: 0001.
DATA: BEGIN OF ITAB OCCURS 1,
EG TYPE P0001-PERSG,
PERNR TYPE PERNR,
TOTAL TYPE I,
END OF ITAB.

DATA: BEGIN OF ITAB_main OCCURS 1,
PERNR TYPE PERNR,
EG TYPE P0001-PERSG,
TOTAL TYPE I,
END OF ITAB_main.

data: flag,flag1.

START-OF-SELECTION.

GET PERNR.
  RP_PROVIDE_FROM_LAST P0001 SPACE PN-BEGDA PN-ENDDA.
  MOVE PERNR-PERNR TO ITAB-PERNR.
  MOVE P0001-PERSG TO ITAB-EG.
  CLEAR ITAB-TOTAL.
  ITAB-TOTAL = ITAB-TOTAL + 1.
  APPEND ITAB.
 
END-OF-SELECTION.
  SORT ITAB BY EG .
  data: tot type i.
  data: tabix type sy-tabix.
  clear tot.

  clear: flag,flag1.

  loop at itab.
    tot = tot + itab-total.
    MOVE-CORRESPONDING itab to itab_main.
    APPEND itab_main.
    at end of eg.
      if itab-eg ne '1' and flag eq ' '.
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
      elseif itab-eg eq '3' .
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
        flag1 = 'X'.
      elseif itab-eg ne '4' and itab-eg ne '5' and itab-eg ne '6' and itab-eg ne '7' and itab-eg ne '8' and itab-eg ne '9' and itab-eg ne '10' and itab-eg ne '11' and flag1 eq 'X'.
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
        flag1 = 'X'.
      endif.
    endat.
  endloop.

  LOOP AT ITAB_main.
    WRITE: / ITAB_main-PERNR ,20 ITAB_main-EG ,25 ITAB_main-TOTAL.
  ENDLOOP.

Regards

Sajid

10 REPLIES 10
Read only

Former Member
0 Likes
1,365

Hi,

Did you try using SUM statement.

Find the Help of [SUM|http://help.sap.com/saphelp_nw04/helpdata/EN/fc/eb381a358411d1829f0000e829fbfe/content.htm]. I think this should resolve the issue.

Regards

Sarves

Read only

0 Likes
1,365

Hi Sarves,

That is ok

But we can't use at new statement as i want the sum of emp group 1 ,2 at one place ,

More over how to insert only total field to be filled rest of fields should be empty

Regards

sas

Read only

0 Likes
1,365

Hi, SAS

Please Test the following Sample Code it is working fine and hope will solve out your problem,

TYPES:  BEGIN OF ty,
        persg LIKE pa0001-persg, " persg must be left most
        pernr LIKE pa0001-pernr,
        amount TYPE i,
        END OF ty.

DATA: it1 TYPE STANDARD TABLE OF ty,
      it2 TYPE STANDARD TABLE OF ty,
      wa LIKE LINE OF it1.

wa-persg = '1'. wa-pernr = '1'. wa-amount = 1234. APPEND wa TO it1.
wa-persg = '2'. wa-pernr = '2'. wa-amount = 10.   APPEND wa TO it1.
wa-persg = '3'. wa-pernr = '3'. wa-amount = 15.   APPEND wa TO it1.
wa-persg = '3'. wa-pernr = '4'. wa-amount = 2343. APPEND wa TO it1.
wa-persg = '4'. wa-pernr = '5'. wa-amount = 1000. APPEND wa TO it1.
wa-persg = '4'. wa-pernr = '6'. wa-amount = 1000. APPEND wa TO it1.

SORT: it1 BY persg.

LOOP AT it1 INTO wa WHERE persg = 1 OR persg = 2.
  wa-persg = '1'. " because we Need 1 and 2 togather
  MODIFY: it1 FROM wa INDEX sy-tabix.
ENDLOOP.

LOOP AT it1 INTO wa.
  APPEND wa TO it2.
  AT END OF persg.
    SUM.
    wa-pernr = '99999999'.
    APPEND: wa TO it2.
  ENDAT.
ENDLOOP.

LOOP AT it2 INTO wa.
  IF wa-pernr <> '99999999'.
    WRITE: / wa-persg, wa-amount.
  ELSE.
    WRITE: /1(7) 'Sub T', 8 wa-amount LEFT-JUSTIFIED.
  ENDIF.
ENDLOOP.

Please Reply if any Issue,

Best Regards,

Faisal

Read only

0 Likes
1,365

Hi sas,

It is not possible to do that in internal table level.

thanks

Naresh

Read only

Former Member
0 Likes
1,365

Hi i have corrected and tested your code.

You can use this code,did according to your logic

 
END-OF-SELECTION.
SORT ITAB BY EG .
data: tot type i.
data: tabix type sy-tabix.
clear tot.
loop at itab.
tot = tot + itab-total.
at end of eg.
if itab-eg ne 1.
  itab-total = tot.
  tabix = sy-tabix + 1.
insert itab index tabix.
clear tot.
endif.
endat.
endloop.

LOOP AT ITAB.
WRITE: / ITAB-PERNR ,20 ITAB-EG ,25 ITAB-TOTAL.
ENDLOOP.

Regards

Sajid

Edited by: shaik sajid on Jul 14, 2009 7:46 AM

Edited by: shaik sajid on Jul 14, 2009 8:17 AM

Read only

0 Likes
1,365

>

> Hi i have corrected and tested your code.

>

> You can use this code,did according to your logic

>

>

 
> END-OF-SELECTION.
> SORT ITAB BY EG .
> data: tot type i.
> data: tabix type sy-tabix.
> clear tot.
> loop at itab.
> tot = tot + itab-total.
> at end of eg.
> if itab-eg ne 1.
>   itab-total = tot.
>   tabix = sy-tabix + 1.
> insert itab index tabix.
> clear tot.
> endif.
> endat.
> endloop.
> 
> LOOP AT ITAB.
> WRITE: / ITAB-PERNR ,20 ITAB-EG ,25 ITAB-TOTAL.
> ENDLOOP.

>

> Regards

> Sajid

>

HI Shaid,

This solution is providing dump at the insert buton please provide the alternate solution

as this is not only restricted to emp group 1-2 one categaroy.

actually the requriment is that emp gorup 1-2 , 3 , 4-12 is the exact requirement ....Solution still pending !!

Sas

HI

Read only

Former Member
0 Likes
1,365

Hi,

Add another field with name total ,make use of control break statement AT END OF field(in your case field is employee group).

In side this control bake statement sum up the employee group and pass the value to total in the work area of the internal table and modify the in ternal using the work area.

Now you will the sub total inside the internal table .

Hope this solution should solve the problem.

Regards,

Madhava

Read only

Former Member
0 Likes
1,366

Hi,

try it now.

REPORT YTEST_XLS_COL LINE-SIZE 300.

TABLES: PERNR.
INFOTYPES: 0001.
DATA: BEGIN OF ITAB OCCURS 1,
EG TYPE P0001-PERSG,
PERNR TYPE PERNR,
TOTAL TYPE I,
END OF ITAB.

DATA: BEGIN OF ITAB_main OCCURS 1,
PERNR TYPE PERNR,
EG TYPE P0001-PERSG,
TOTAL TYPE I,
END OF ITAB_main.

data: flag,flag1.

START-OF-SELECTION.

GET PERNR.
  RP_PROVIDE_FROM_LAST P0001 SPACE PN-BEGDA PN-ENDDA.
  MOVE PERNR-PERNR TO ITAB-PERNR.
  MOVE P0001-PERSG TO ITAB-EG.
  CLEAR ITAB-TOTAL.
  ITAB-TOTAL = ITAB-TOTAL + 1.
  APPEND ITAB.
 
END-OF-SELECTION.
  SORT ITAB BY EG .
  data: tot type i.
  data: tabix type sy-tabix.
  clear tot.

  clear: flag,flag1.

  loop at itab.
    tot = tot + itab-total.
    MOVE-CORRESPONDING itab to itab_main.
    APPEND itab_main.
    at end of eg.
      if itab-eg ne '1' and flag eq ' '.
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
      elseif itab-eg eq '3' .
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
        flag1 = 'X'.
      elseif itab-eg ne '4' and itab-eg ne '5' and itab-eg ne '6' and itab-eg ne '7' and itab-eg ne '8' and itab-eg ne '9' and itab-eg ne '10' and itab-eg ne '11' and flag1 eq 'X'.
        itab_main-total = tot.
        APPEND itab_main.
        clear tot.
        flag = 'X'.
        flag1 = 'X'.
      endif.
    endat.
  endloop.

  LOOP AT ITAB_main.
    WRITE: / ITAB_main-PERNR ,20 ITAB_main-EG ,25 ITAB_main-TOTAL.
  ENDLOOP.

Regards

Sajid

Read only

0 Likes
1,365

shaik,

Please check this once ....

It is coming wrong

Regards

sas

Read only

0 Likes
1,365

Hi,

Its working correctly in my system (but may there is no enough data in my system).

So you can use the code i mentioned, for the logic top work i declared one another internal table and i changed the sequence of structure in first internal table (for endat to work).

You may be checking with realtime data, so u may find small bugs, so you can correct them with little change in the logic.

Regards

Sajid