Application Development 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: 

how to print the subtotal in the alv

Former Member
0 Kudos

Hi all,

I got a requirement ..

i got matnr . quantity.

material1 1000kg

material1 2000kg

material1 3000kg .

material2 100kg

material2 200kg

materail2 300 kg

i need to print the subtotal based upon the material number in ALV . So my ouput should look like ....

material1 1000kg

material1 2000kg

material1 3000kg .

6000kg

material2 100kg

material2 200kg

materail2 300 kg

600kg

I am not able to do , i have gone through the forums , but i am able to print the Grand totals .

I need to print the sub totals . Please let me know ....

Regards

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

U can do this..

in the fieldcatalod do this for column 'MATNR'


is_fieldcat-do_sum    = 'X'.             "<------for subtotal

For IT_SORT do this...


  is_sort-spos      = <position>.
  is_sort-fieldname = 'MATNR'.
  is_sort-tabname   = 'IT_FINAL'.
  APPEND is_sort TO it_sort.
  CLEAR is_sort.

and pass these to ALV


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      is_layout               = is_layout
      it_fieldcat             = it_fieldcat
      i_save                  = 'A'
      it_sort                 = it_sort          "<-------pass Sort
    TABLES
      t_outtab                = it_final
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.

5 REPLIES 5

former_member181995
Active Contributor
0 Kudos

Press F1 on At end of,

On change of.

Or search in SCN for Control break statements.

Former Member
0 Kudos

Hi,

I think material and quantity are in one internal table, so one solution you can do is to loop this internal table and do the at new statement.

Before you do that, you should sort the table by material.

Like that:

sort itab by matnr.
 loop at itab into wa.
   append wa to itab_new. (using a new internal table, structure is the same as itab)
   at new matnr.
    sum.  (this is the subtotal for the same material)
    wa-matnr = ''.
    append wa to itab_new.    
  endat.
endloop.

Then you can use ALV to display the itab_new.

Hope it helps.

Regards,

Chris Gu

Edited by: Gu Chris on Nov 17, 2008 2:54 AM

Former Member
0 Kudos

hi raj,

you can do the sub total in your lay-out, just sort per material then tick the subtotal.

Former Member
0 Kudos

Dear Raj

You can do it with your fieldcatalog, check my code properly.


REPORT ZTEST_ALV_TEXT .
type-pools : slis.
 
types : begin of itab_t,
ebeln like ekpo-ebeln,
lifnr like ekko-lifnr,
ekorg like ekko-ekorg,
ekgrp like ekko-ekgrp,
werks like ekpo-werks,
ebelp like ekpo-ebelp,
matnr like ekpo-matnr,
menge like ekpo-menge,
netpr like ekpo-netpr,
d, "Dummy field to fire the Subtotal text event
end of itab_t.
 
data: itab type table of itab_t.
data: tab type itab_t.
data : itab1 like eket occurs 0 with header line.
 
data: t_fcat type slis_t_fieldcat_alv,
it_sort type slis_t_sortinfo_alv,
t_events type slis_t_event,
listhead type slis_t_listheader,
ls_layout type slis_layout_alv.
 
start-of-selection.
 
select a~ebeln
a~lifnr
a~ekorg
a~ekgrp
b~werks
b~ebelp
b~matnr
b~menge
b~netpr
up to 100 rows
into corresponding fields of table itab
from ekko as a inner join ekpo as b
on a~ebeln = b~ebeln.
 
end-of-selection.
 
perform fill_fcat using t_fcat.
 
perform fill_event using t_events.
 
perform fill_layout.
 
perform display.
 
form fill_fcat using p_t_fcat type slis_t_fieldcat_alv.
 
data : lfcat type slis_fieldcat_alv,
 
colpos type i value '0'.
 
data : ls_sort type slis_sortinfo_alv.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EBELN'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELN'.
lfcat-ref_tabname = 'EKKO'.
lfcat-hotspot = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'LIFNR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'LIFNR'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EKORG'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EKORG'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EKGRP'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EKGRP'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'WERKS'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'WERKS'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EBELP'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELP'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'MATNR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'MATNR'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'MENGE'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'MENGE'.
lfcat-ref_tabname = 'EKPO'.
lfcat-do_sum = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'NETPR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'NETPR'.
lfcat-ref_tabname = 'EKPO'.
lfcat-do_sum = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'D'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELN'.
lfcat-ref_tabname = 'EKKO'.
lfcat-no_out = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
ls_sort-spos = 1.
ls_sort-fieldname = 'EBELN'.
ls_sort-tabname = 'ITAB'.
ls_sort-up = 'X'.
ls_sort-group = 'UL'.
 
 
append ls_sort to it_sort.
clear ls_sort.
 
ls_sort-spos = 2.
ls_sort-fieldname = 'D'.
ls_sort-tabname = 'ITAB'.
ls_sort-up = 'X'.
ls_sort-group = 'UL'.
ls_sort-subtot = 'X'.
 
append ls_sort to it_sort.
endform. " fill_fcat
 
 
form fill_event using p_t_events type slis_t_event.
 
data : ls_event type slis_alv_event.
 
call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = p_t_events
 
EXCEPTIONS 
LIST_TYPE_WRONG = 1 
OTHERS = 2 
.
if sy-subrc ne 0.
 
 
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 
endif.
 
read table p_t_events with key name = slis_ev_top_of_page
into ls_event.
if sy-subrc = 0.
move 'TOP_OF_PAGE' to ls_event-form.
append ls_event to p_t_events.
endif.
 
read table p_t_events with key name = SLIS_EV_SUBTOTAL_TEXT
into ls_event.
if sy-subrc = 0.
move 'SUBTOTAL' to ls_event-form.
append ls_event to p_t_events.
endif.
 
endform. " FILL_EVENT
 
FORM SUBTOTAL USING I_LISTHEAD STRUCTURE tab
I_SUBTOTAL TYPE SLIS_SUBTOT_TEXT.
 
*criteria type slis_fieldname,
 
keyword like dd03p-reptext, 
criteria_text(255) type c, 
max_len like dd03p-outputlen, 
display_text_for_subtotal(255) type c, 
if I_SUBTOTAL-criteria = 'D'.
I_SUBTOTAL-display_text_for_subtotal = 'Sub total'.
 "here you need to do some calculations and assing the data 
"back to total line
endif.
 
ENDFORM.
 
form top_of_page.
 
data : s_listhead type slis_listheader.
 
clear s_listhead.
s_listhead-typ = 'H'.
s_listhead-info = 'SIMPLE REPORT'.
append s_listhead to listhead.
 
s_listhead-typ = 'S'.
s_listhead-key = 'EBELN'.
s_listhead-info = 'ALV'.
append s_listhead to listhead.
 
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = listhead
i_logo = 'ENJOYSAP_LOGO'
 
 
I_END_OF_LIST_GRID = 
.
endform. "TOP_OF_PAGE
 
form fill_layout .
 
ls_layout-zebra = 'X'.
ls_layout-detail_popup = 'X'.
ls_layout-key_hotspot = 'X'.
ls_layout-window_titlebar = 'Test Title'.
ls_layout-totals_text = 'GRAND TOTAL'.
ls_layout-subtotals_text = 'SUB'.
 
endform. " fill_layout
 
form display .
 
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
i_callback_program = sy-repid
it_sort = it_sort[]
it_events = t_events
tables
t_outtab = itab
 
EXCEPTIONS 
PROGRAM_ERROR = 1 
OTHERS = 2 
.
if sy-subrc ne 0.
 
MESSAGE ID SY-MSGI D TYPE SY-MSGTY NUMBER SY-MSGNO 
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 
endif.
endform. " display

Hope this will help you to close your thrend

Former Member
0 Kudos

Hi,

U can do this..

in the fieldcatalod do this for column 'MATNR'


is_fieldcat-do_sum    = 'X'.             "<------for subtotal

For IT_SORT do this...


  is_sort-spos      = <position>.
  is_sort-fieldname = 'MATNR'.
  is_sort-tabname   = 'IT_FINAL'.
  APPEND is_sort TO it_sort.
  CLEAR is_sort.

and pass these to ALV


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-repid
      is_layout               = is_layout
      it_fieldcat             = it_fieldcat
      i_save                  = 'A'
      it_sort                 = it_sort          "<-------pass Sort
    TABLES
      t_outtab                = it_final
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.