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

Doubt in abap coding

Former Member
0 Likes
2,183

Hi Experts,

I have doubt, i have an internal table with four column. like below.

KSCHL VALUE DIFF

ZMBO 1.25 12.56

ZMBO 2.31 12.56

ZMBO 4 12.56

ZMBO 5 12.56

ZCHL 4.31 16.87

ZCHL 5.21 16.87

ZCHL 7.35 16.87

ZNLM 0.005 8.03

ZNLM 0.125 8.03

ZNLM 3.65 8.03

ZNLM 4.25 8.03

Now i want to print the above internal table like below,

KSCHL VALUE DIFF

ZMBO 1.25 12.56

ZMBO 2.31 12.56

ZMBO 4 12.56

ZMBO 5 12.56

ZMBO TOTAL 12.56

ZCHL 4.31 16.87

ZCHL 5.21 16.87

ZCHL 7.35 16.87

ZCHL TOTAL 16.87

ZNLM 0.005 8.03

ZNLM 0.125 8.03

ZNLM 3.65 8.03

ZNLM 4.25 8.03

ZNLM TOTAL 8.03

How to print this? Can any one help this. Point will be sure.

Mohana

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,143

Hi,

sort itab[] by kschl.

Loop at itab.

write:itab-kschl, itab-value, itab-Diff.

at end of kschl.

write:itab-kschl, 'Total' itab-Diff.

endat.

Endloop.

Rgds,

Bujji

23 REPLIES 23
Read only

Former Member
0 Likes
2,143

Hi,

Use AT END OF Statement in your loop. Go through the documentation to understand how this statement should be used.

sort the itab by KSCHL.

Loop at itab.

write:\ ....ur data.

At end of kschl.

write:/ 'ZMBO TOTAL 12.56'.

endat.

endloop.

Shruthi

Read only

0 Likes
2,143

Hi,

Other way is shown below

sort by kschl.

read table itab index 1 .

l_kschl = itab-kschl.

Loop at itab.

if l_kschl = itab-kschl.

write:/ ur data...

else.

write"/ l_kschl 'TOTAL 12.56' " the total

l_kschl = itab-kschl.

endif.

endloop.

Shruthi

Edited by: Shruthi R on Jun 3, 2008 1:11 PM

Read only

Former Member
0 Likes
2,143

You can use:

AT END OF KSCHL.

WRITE: 'TOTAL' ....

SKIP.

Read only

Former Member
0 Likes
2,143

Hi ,

sort your internal table.

Use control break statements. at end of kschl you print the sum record.

Reward if find useful

Read only

Former Member
0 Likes
2,143

Hi,

U can use,

Loop at it_tab into wa_tab.

lv_value = wa_tab-value.

lv_diff = wa_tab-diff.

at end of kschl.

wa_tab1-kschl = wa_tab-kschl.

wa_tab1-value = lv_value.

wa_tab1-diff = lv_diff.

append wa_tab1 to it_tab1.

clear wa_tab1.

end of.

clear wa_tab.

end loop.

thanks

Read only

Former Member
0 Likes
2,144

Hi,

sort itab[] by kschl.

Loop at itab.

write:itab-kschl, itab-value, itab-Diff.

at end of kschl.

write:itab-kschl, 'Total' itab-Diff.

endat.

Endloop.

Rgds,

Bujji

Read only

0 Likes
2,143

Hi

I tried at end of kschl. bt its giving like below,

ZMBO 1.25 12.56

ZMBO Total 12.56

ZMBO 2.31 12.56

ZMBO Total 12.56

ZMBO 4 12.56

ZMBO Total 12.56

ZMBO 5 12.56

ZMBO Total 12.56

ZCHL 4.31 16.87

ZCHL TOTAL 16.87

ZCHL 5.21 16.87

ZCHL TOTAL 16.87

ZCHL 7.35 16.87

ZCHL TOTAL 16.87

Bt i am not expecting this way.

i need after every group i need that total.

Plz help this .

Mohana

Read only

0 Likes
2,143

Hi,

Did you sort your internal table by KSCHL before writing the data.

Rgds,

Bujji

Read only

0 Likes
2,143

Hi Bujj,

I sorted the internal table with kschl before the loop. but still its giving like the above output only.

Plz help this issue. Plzzzz

Thnx,

Mohana

Read only

0 Likes
2,143

Hi,

Loop at itab.

IF sy-tabix = 1.

lv_kschl = itab-kschl.

ENDIF.

IF lv_kschl <> itab-kschl.

write:itab-kschl, 'Total' itab-Diff.

lv_kschl = itab-kschl.

ENDIF.

write:itab-kschl, itab-value, itab-Diff.

Endloop.

Rgds,

Bujji

Read only

0 Likes
2,143

Hi,

The above logic writes only the first row of tht internal table, its not writing the diff total under each group..

Plz help this issue.

maximum point will be sure.

Mohana

Read only

0 Likes
2,143

Hi,

Loop at itab.

IF sy-tabix = 1.

lv_kschl = itab-kschl.

ENDIF.

IF lv_kschl NE itab-kschl.

write:itab-kschl, 'Total' itab-Diff.

lv_kschl = itab-kschl.

ENDIF.

write:itab-kschl, itab-value, itab-Diff.

Endloop.

Rgds,

Bujji

Edited by: Bujji on Jun 3, 2008 1:34 PM

Read only

0 Likes
2,143

Hi,

Its giving below output, i think we are very close to the output. Plz hlep me,

ZMBO Total 12.56

ZMBO 2.31 12.56

ZMBO 4 12.56

ZMBO 5 12.56

ZCHL TOTAL 16.87

ZCHL 4.31 16.87

ZCHL 5.21 16.87

ZCHL 7.35 16.87

Its not writing under the Each group. Its writing the value above the each group. i need the total under each group..

Plz help this...

Mohana

Read only

0 Likes
2,143

Hi,

Loop at itab.

write:itab-kschl, itab-value, itab-Diff.

index = sy-tabix + 1.

clear wa_temp.

Read table itab into wa_temp index index.

IF wa_temp-kschl NE itab-kschl.

write:itab-kschl, 'Total' itab-Diff.

ENDIF.

Endloop.

Rgds,

Bujji

Edited by: Bujji on Jun 3, 2008 1:49 PM

Read only

0 Likes
2,143

Hi Bujj,

i am not getting the sentence

Read table itab into wa_temp index index.

can plz tell correct formet? Plz...

Mohana

Read only

0 Likes
2,143

Hi,

In the loop itself you are reading the next line and compare the next kschl with the current kschl. Index should be the value of sy-tabix + 1.

Has your problem got solved or not?

Rgds,

Bujji

Read only

0 Likes
2,143

Hi Bujji,

No its not solved, its giving error onthis line.

Read table itab into wa_temp index index.

Plz help the correct syntax line...

Plzzzz

Read only

0 Likes
2,143

Hi,

data:wa_temp like line of itab.

Loop at itab.

write:itab-kschl, itab-value, itab-Diff.

move-corresponding itab to wa_temp.

tabix = sy-tabix + 1.

Read table itab index tabix.

IF wa_temp-kschl NE itab-kschl.

write:wa_temp-kschl, 'Total' wa_temp-Diff.

ENDIF.

Endloop.

Rgds,

Bujji

Read only

0 Likes
2,143

hi Mohana,

pls. go for the solution suggested by Supriya!!! All other posts are useless!!! You should only enter the following line before the LOOP:

SORT itab BY KSCHL.

hope this helps

ec

Read only

0 Likes
2,143

This works for me


DATA:
  BEGIN OF myrec,
    kschl        TYPE kschl,
    value        TYPE p DECIMALS 3,
    diff         TYPE p DECIMALS 3,
  END OF myrec,
  itab LIKE STANDARD TABLE OF myrec.
DATA: sv_diff    TYPE p DECIMALS 3.

START-OF-SELECTION.

  PERFORM build_table.

  SORT itab.

  LOOP AT itab INTO myrec.
    WRITE:/ myrec-kschl, myrec-value, myrec-diff.
    sv_diff = myrec-diff.
    AT END OF kschl.
      WRITE:/ 'Total', myrec-kschl, sv_diff.
      SKIP.
    ENDAT.
  ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  build_table
*&---------------------------------------------------------------------*
FORM build_table.
  myrec-kschl = 'ZMBO'. myrec-value = '1.25'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '2.31'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '4.00'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '5.00'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '4.31'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '5.21'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '7.35'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '0.005'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '0.125'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '3.65'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '4.25'. myrec-diff = '8.03'.
  APPEND myrec TO itab.


ENDFORM.                    " build_table

And the output


ZCHL            7.350            16.870      
ZCHL            5.210            16.870      
ZCHL            4.310            16.870      
Total ZCHL           16.870                  
                                             
ZMBO            1.250            12.560      
ZMBO            2.310            12.560      
ZMBO            4.000            12.560      
ZMBO            5.000            12.560      
Total ZMBO           12.560                  
                                             
ZNLM            4.250             8.030      
ZNLM            3.650             8.030      
ZNLM            0.125             8.030      
ZNLM            0.005             8.030      
Total ZNLM            8.030                  

Read only

0 Likes
2,143

Want headings?


DATA:
  BEGIN OF myrec,
    kschl        TYPE kschl,
    value(4)     TYPE p DECIMALS 3,
    diff(4)      TYPE p DECIMALS 3,
  END OF myrec,
  itab LIKE STANDARD TABLE OF myrec.
DATA: sv_diff(4)    TYPE p DECIMALS 3.

TOP-OF-PAGE.
  WRITE:/1 'KSCHL',
         10 'Value',
         25 'Diff'.

START-OF-SELECTION.

  PERFORM build_table.

  SORT itab.

  NEW-PAGE.
  LOOP AT itab INTO myrec.
    WRITE:/ myrec-kschl UNDER 'KSCHL',
            myrec-value UNDER 'Value',
            myrec-diff  UNDER 'Diff'.
    sv_diff = myrec-diff.
    AT END OF kschl.
      WRITE:/ 'Total', myrec-kschl,
            sv_diff under 'Diff'.
      SKIP.
    ENDAT.
  ENDLOOP.

*&---------------------------------------------------------------------*
*&      Form  build_table
*&---------------------------------------------------------------------*
FORM build_table.
  myrec-kschl = 'ZMBO'. myrec-value = '1.25'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '2.31'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '4.00'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZMBO'. myrec-value = '5.00'. myrec-diff = '12.56'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '4.31'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '5.21'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZCHL'. myrec-value = '7.35'. myrec-diff = '16.87'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '0.005'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '0.125'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '3.65'. myrec-diff = '8.03'.
  APPEND myrec TO itab.
  myrec-kschl = 'ZNLM'. myrec-value = '4.25'. myrec-diff = '8.03'.
  APPEND myrec TO itab.

ENDFORM.                    " build_table

And the Output


KSCHL    Value          Diff         
ZCHL        7.350         16.870     
ZCHL        5.210         16.870     
ZCHL        4.310         16.870     
Total ZCHL                16.870     
                                     
ZMBO        1.250         12.560     
ZMBO        2.310         12.560     
ZMBO        4.000         12.560     
ZMBO        5.000         12.560     
Total ZMBO                12.560     
                                     
ZNLM        4.250          8.030     
ZNLM        3.650          8.030     
ZNLM        0.125          8.030     
ZNLM        0.005          8.030     
Total ZNLM                 8.030     
                                     

Read only

Former Member
0 Likes
2,143

Hi,

May this will help you Out.

LOOP AT i_tab.

WRITE : i_tab-kschl.

WRITE : i_tab-value.

WRITE : i_tab-diff.

WRITE :/.

AT END OF kschl.

SUM.

WRITE : i_tab-kschl.

WRITE : 'Total' .

WRITE : i_tab-value.

WRITE :/.

ENDAT.

ENDLOOP.

Regards

Supriya Bhatt

Read only

0 Likes
2,143

Hi ,

Continue from Last message...

Suppose i_tab is your Internal table consisting of:

ZMBO 1.25 12.56

ZMBO 2.31 12.56

ZMBO 4.00 12.56

ZMBO 5.00 12.56

ZCHL 4.31 16.87

ZCHL 5.21 16.87

ZCHL 7.35 16.87

ZNLM 0.005 8.03

ZNLM 0.125 8.03

ZNLM 3.65 8.03

ZNLM 4.25 8.03

Then write the code:

LOOP AT i_tab.

WRITE : i_tab-kschl.

WRITE : i_tab-value.

WRITE : i_tab-diff.

WRITE :/.

AT END OF kschl.

SUM.

WRITE : i_tab-kschl.

WRITE : 'Total' .

WRITE : i_tab-value.

WRITE :/.

ENDAT.

ENDLOOP.

Will give the below output: May this will help you

ZMBO 1.250 12.56

ZMBO 2.31 12.56

ZMBO 4.000 12.56

ZMBO 5.000 12.56

ZMBO Total 12.56

ZCHL 4.31 16.87

ZCHL 5.21 16.87

ZCHL 7.35 16.87

ZCHL Total 16.87

ZNLM 0.005 8.03

ZNLM 0.12 8.03

ZNLM 3.65 8.03

ZNLM 4.25 8.03

ZNLM Total 8.03

Regards:

Supriya Bhatt

Edited by: Supriya Bhatt on Jun 3, 2008 1:59 PM

Edited by: Supriya Bhatt on Jun 3, 2008 1:59 PM