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 record inside the internal table in below code

Former Member
0 Likes
904

Hi all,

My requirement is to find the sub-total and need to insert in between the internal table.

You can see the output ....where I want the sub-total F2 when 1 & 2 combindely , 3 , 4& 5 combindely .Please check it and let me know is it possible

when i am using modification it is not creating extra row inside the table instead it is modifying one row and putting the total there.

For ex: the origianl output is

F1 F2 F3

A 1 1

B 1 1

F 2 1

D 3 1

E 4 1

C 5 1

We want to display all the total of f2 of 1-2 , 3 , 4-5

so expcected output is

F1 F2 F3

A 1 1

B 1 1

F 2 1

  • * 3 ->This is the sub-total of 1& 2 of f2

D 3 1

  • * 1 ->this is the sub-total of 3

E 4 1

C 5 1

  • * 2 -> this is the sub-total of 4 & 5

  • = space

But coming output is

A 1 1

B 1 1

  • * 3 -> it is modifying the F row and inserting the total .Total is comong correct but is shoule insert instead of modifying the record!!

  • * 1

E 4 1

  • * 2

Please help how to insert the row total at the end of the chage of field

Please find the below code ..Due to space problem i am attaching below

Sas

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
854

SaS,

You might have to do something [similer|; where i was intended to do so for Sub-total text.

7 REPLIES 7
Read only

Former Member
0 Likes
854

REPORT  YTEST_MODIFY.

data: begin of itab occurs 1,
TOT TYPE C,
f1 type c,
f2 type c,
f3 type i ,
end of itab.

data: begin of Jtab occurs 1,
f1 type c,
f2 type c,
f3 type i ,
end of Jtab.
START-OF-SELECTION.
ITAB-f1 = 'A'.ITAB-F2 =  1.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'B'.ITAB-F2 =  1.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'C'.ITAB-F2 =  5.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'D'.ITAB-F2 =  3.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'E'.ITAB-F2 =  4.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'F'.ITAB-F2 =  2.ITAB-F3 =  1.
APPEND ITAB.
SORT ITAB BY F2.

LOOP AT ITAB.
WRITE:/ ITAB-F1 ,8 ITAB-F2 , ITAB-F3.
ENDLOOP.

LOOP AT ITAB.
IF ITAB-F2 = 1 OR ITAB-F2 = 2.
ITAB-TOT = 1.
MODIFY ITAB.
ELSEIF ITAB-F2 = 3.
ITAB-TOT = 3.
MODIFY ITAB.
ELSEIF ITAB-F3 = 4 OR ITAB-F3 = 5.
ITAB-TOT = 4.
MODIFY ITAB.
ENDIF.
ENDLOOP.

DATA: A TYPE I , B .

LOOP AT ITAB.
IF B = 0.
A = A + ITAB-F3.
ENDIF.
AT END OF TOT.
MOVE SPACE TO ITAB-F1.
MOVE SPACE TO ITAB-F2.
MOVE A TO ITAB-F3.
MODIFY ITAB .
CLEAR A.
B = 1.
ENDAT.

B = 0.
ENDLOOP.



LOOP AT ITAB.
MOVE-CORRESPONDING ITAB TO JTAB.
APPEND JTAB.
ENDLOOP.

ULINE.
LOOP AT JTAB.
WRITE: / JTAB-F1 , JTAB-F2 , JTAB-F3.
ENDLOOP.
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
854

by which field you are grouping

1and 2

3

4 and 5

Read only

0 Likes
854

hi Kulesh,

Consider F2 as Emp group where we HR need to identify the totals of emp gorup 1& 2 and 3 and 4&5 how many people are there.,....

So this is my requirement and F3 is the Total field which need to be inserted in between of the internal table...

So i hope you understand my requirement.....

Amit that thread is TOO vast and too complicated i guess...

Actually a small change i need to put in my previous code...

instead of modify i need to put insert is working but it is inserting above the end of Field...

But to insert in the appropraite line how ???will copy paste the code in next thread

Sas

Read only

0 Likes
854

Here is the solution ....i Got the answer Thanks for your helping hands friends


REPORT  YTEST_MODIFY.

DATA: BEGIN OF ITAB OCCURS 1,
TOT TYPE C,
F1 TYPE C,
F2 TYPE C,
F3 TYPE I ,
END OF ITAB.

DATA: BEGIN OF JTAB OCCURS 1,
F1 TYPE C,
F2 TYPE C,
F3 TYPE I ,
END OF JTAB.



START-OF-SELECTION.


  ITAB-F1 = 'A'.
  ITAB-F2 =  1.
  ITAB-F3 =  1.
  APPEND ITAB.

  ITAB-F1 = 'B'.
  ITAB-F2 =  1.
  ITAB-F3 =  1.
  APPEND ITAB.

  ITAB-F1 = 'C'.
  ITAB-F2 =  5.
  ITAB-F3 =  1.
  APPEND ITAB.

  ITAB-F1 = 'D'.
  ITAB-F2 =  3.
  ITAB-F3 =  1.
  APPEND ITAB.

  ITAB-F1 = 'E'.
  ITAB-F2 =  4.
  ITAB-F3 =  1.
  APPEND ITAB.

  ITAB-F1 = 'F'.
  ITAB-F2 =  2.
  ITAB-F3 =  1.
  APPEND ITAB.

  SORT ITAB BY F2.

  LOOP AT ITAB.
    WRITE:/1 ITAB-F1 ,
          8 ITAB-F2 ,
          10 ITAB-F3 .
  ENDLOOP.

  LOOP AT ITAB.
    IF ITAB-F2 = 1 OR ITAB-F2 = 2.
      ITAB-TOT = 1.
      MODIFY ITAB.
    ELSEIF ITAB-F2 = 3.
      ITAB-TOT = 3.
      MODIFY ITAB.
    ELSEIF ITAB-F2 = 4 OR ITAB-F2 = 5.
      ITAB-TOT = 4.
      MODIFY ITAB.
    ENDIF.
  ENDLOOP.


  SKIP 2.

  SORT ITAB BY TOT.

  DATA : L_SUM(2) TYPE C,
         L_ROW(2) TYPE C.

  LOOP AT ITAB.
    MOVE-CORRESPONDING ITAB TO JTAB.
    APPEND JTAB.
    L_SUM = L_SUM + ITAB-F3 .

    AT END OF TOT.
      CLEAR JTAB.
      JTAB-F3 = L_SUM .
      APPEND JTAB.
      CLEAR L_SUM.
    ENDAT.

  ENDLOOP.

  LOOP AT JTAB.
    WRITE:/1 JTAB-F1 ,
          8 JTAB-F2 ,
          10 JTAB-F3 .
  ENDLOOP.

*  DATA: a TYPE i , b .
*
*  LOOP AT itab.
*    IF b = 0.
*      a = a + itab-f3.
*    ENDIF.
*    AT END OF tot.
*      MOVE space TO itab-f1.
*      MOVE space TO itab-f2.
*      MOVE a TO itab-f3.
*      MODIFY itab .
*      CLEAR a.
*      b = 1.
*    ENDAT.
*
*    b = 0.
*  ENDLOOP.
*
*
*
*  LOOP AT itab.
*    MOVE-CORRESPONDING itab TO jtab.
*    APPEND jtab.
*  ENDLOOP.
*
*  ULINE.
*  LOOP AT jtab.
*    WRITE: / jtab-f1 , jtab-f2 , jtab-f3.
*  ENDLOOP.


Read only

Former Member
0 Likes
855

SaS,

You might have to do something [similer|; where i was intended to do so for Sub-total text.

Read only

Former Member
0 Likes
854
You can see the output ....where I want the sub-total F2 when 1 & 2 combindely , 3 , 4& 5 combindely .

Hi Sas,

On what basis do you decide for which fields of F2 you have to sum F3? as in how have you decided 1-2,3,4-5...?

whats is the sequence?

Regards,

Sumit Nene

Read only

Former Member
0 Likes
854

hii ssaplove,

check the following code, its working in my system..

*&---------------------------------------------------------------------*
*& Report  ZTEST09
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZTEST09.

data total type i.
data n type sy-tabix.
data m type sy-tabix.
data p type sy-tabix.

data: begin of itab occurs 1,
TOT TYPE C,
f1 type c,
f2 type c,
f3 type i ,
end of itab.

data: begin of Jtab occurs 1,
f1 type c,
f2 type c,
f3 type i ,
end of Jtab.
START-OF-SELECTION.
ITAB-f1 = 'A'.ITAB-F2 =  1.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'B'.ITAB-F2 =  1.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'C'.ITAB-F2 =  5.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'D'.ITAB-F2 =  3.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'E'.ITAB-F2 =  4.ITAB-F3 =  1.
APPEND ITAB.
ITAB-f1 = 'F'.ITAB-F2 =  2.ITAB-F3 =  1.
APPEND ITAB.
SORT ITAB BY F2.

loop at itab where f2 = 1 or f2 = 2.
n = sy-tabix.
total = total + itab-f3.
endloop.

n = n + 1.
CLEAR itab.
itab-tot = total.
 insert itab index n.
 clear total.
clear itab.
loop at itab from n where f2 = 3.
  m = sy-tabix.
  total = total + itab-f3.
endloop.


m = m + 1.
CLEAR itab.
itab-tot = total.
 insert itab index m.
 clear total.
clear itab.

loop at itab from m where f2 = 4 or f2 = 5.
  p = sy-tabix.
  total = total + itab-f3.
endloop.

p = p + 1.
CLEAR itab.
itab-tot = total.
 insert itab index p.
 clear total.
clear itab.

 LOOP AT itab.
 write:/ itab-f1 , itab-f2, itab-f3,itab-tot..
 endloop.

*&---------------------------------------------------------------------*
*&      Form  Sub_total
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*