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: 

Subtotal in Alv Grid Display

Former Member
0 Kudos

hi all,

i am creating a report using alv grid display.

In the output i sort a column and want to do subtotal for values corresponding to it in another column.if i separately click on sum and then again subtotal i get it. but i want the format with subtotal when i execute the program itself.

is there any way to do it.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi kavitha,

Check out this Code for field catalogue. Here I am doing Subtotal on basis of LIFNR:

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv with header line,

ls_layout TYPE slis_layout_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv.

lt_fieldcat-ref_tabname = 'it_bsik'.

  • lt_fieldcat-ref_fieldname = 'BUKRS'.

ls_layout-zebra = 'X'.

ls_layout-cell_merge = 'X'.

  • **** SORT ****

ls_sort-fieldname = 'LIFNR'.

ls_sort-up = 'X'.

ls_sort-subtot = 'X'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-up = 'X'.

ls_sort-fieldname = 'GJAHR'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-fieldname = 'BUDAT'.

ls_sort-up = 'X'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-up = 'X'.

ls_sort-fieldname = 'BELNR'.

ls_sort-subtot = ''.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '1'.

ls_fieldcat-outputlen = 20.

ls_fieldcat-seltext_l = 'Document No'.

ls_fieldcat-fieldname = 'BELNR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '2'.

ls_fieldcat-seltext_l = 'Document Date'.

ls_fieldcat-outputlen = 10.

ls_fieldcat-fieldname = 'BUDAT'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '3'.

ls_fieldcat-seltext_l = 'Year'.

ls_fieldcat-fieldname = 'GJAHR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '4'.

ls_fieldcat-seltext_l = 'Vendor Code'.

ls_fieldcat-fieldname = 'LIFNR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '5'.

ls_fieldcat-seltext_l = 'G/L Account'.

ls_fieldcat-fieldname = 'HKONT'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '6'.

ls_fieldcat-reptext_ddic = 'Amount'.

ls_fieldcat-fieldname = 'WRBTR'.

ls_fieldcat-do_sum = 'X'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '7'.

ls_fieldcat-seltext_l = 'Currency Code'.

ls_fieldcat-outputlen = 10.

ls_fieldcat-fieldname = 'WAERS'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '8'.

ls_fieldcat-seltext_l = 'Indicator'.

ls_fieldcat-fieldname = 'SHKZG'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

I_CALLBACK_PF_STATUS_SET = 'SETSTATUS'

I_CALLBACK_USER_COMMAND = 'USRCMD'

IS_LAYOUT = ls_layout

it_fieldcat = lt_fieldcat[]

IT_SORT = lt_sort

I_SAVE = 'A'

TABLES

t_outtab = it_temp[]

  • EXCEPTIONS

  • PROGRAM_ERROR = 1

  • OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

5 REPLIES 5

former_member386202
Active Contributor
0 Kudos

Hi,

Refer this code

&----


*& Form sub_display_data

&----


  • text

----


FORM sub_display_data .

*--To sort the output through material number

DATA : lwa_sort TYPE slis_sortinfo_alv.

DATA : lit_sort TYPE slis_t_sortinfo_alv.

*--Pass the values to the table

lwa_sort-fieldname = 'PERNR'. "Field name in o/p inttable

lwa_sort-tabname = 'it_final2'. "Output Internal table

lwa_sort-spos = '1'. "Sort sequence

lwa_sort-up = 'X'. "Sort in ascending order

lwa_sort-down = ' '. "Sort in descending order

lwa_sort-subtot = 'X'. "Subtotal

APPEND lwa_sort TO lit_sort.

*--Pass the values to the table

lwa_sort-fieldname = 'WORKDATE'. "Field name in o/p inttable

lwa_sort-tabname = 'it_final2'. "Output Internal table

lwa_sort-spos = '2'. "Sort sequence

lwa_sort-up = 'X'. "Sort in ascending order

lwa_sort-down = ' '. "Sort in descending order

lwa_sort-subtot = ' '. "Subtotal

APPEND lwa_sort TO lit_sort.

*--Pass the values to the table

lwa_sort-fieldname = 'WEKLY'. "Field name in o/p inttable

lwa_sort-tabname = 'it_final2'. "Output Internal table

lwa_sort-spos = '3'. "Sort sequence

lwa_sort-up = 'X'. "Sort in ascending order

lwa_sort-down = ' '. "Sort in descending order

lwa_sort-subtot = ' '. "Subtotal

APPEND lwa_sort TO lit_sort.

wa_layout-colwidth_optimize = 'X'.

IF NOT it_final2[] IS INITIAL.

*--Call the function module to display the ALV report

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = wa_layout

i_callback_program = v_repid

it_fieldcat = it_fieldcat1[]

i_default = c_chk

i_save = c_save

it_sort = lit_sort

TABLES

t_outtab = it_final2

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ELSE.

*--Message No data found

MESSAGE i888 WITH text-017.

LEAVE LIST-PROCESSING.

ENDIF.

ENDFORM. " sub_display_data

Regards,

PRashant

0 Kudos

Hi Prashanth,

thanks for the code. i tried but then only after i click on the summation button i get the sub total not directly when i execute the report. will it work only this way .

Former Member
0 Kudos

Hi,

Do as below :


data : gt_sort like slis_t_sortinfo_alv .
         gs_sort type slis_sortinfo_alv.


gs_sort-fieldname = 'Fieldname'.
gs_sort-sortup = 'X'.
gs_sort-subtot = 'X'.
append gs_sort to gt_sort.

And pass gt_sort to IT_SORT of ALV FM exporting parameter.

Thanks,

Sriram Ponna.

Former Member
0 Kudos

Hi kavitha,

Check out this Code for field catalogue. Here I am doing Subtotal on basis of LIFNR:

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv with header line,

ls_layout TYPE slis_layout_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv.

lt_fieldcat-ref_tabname = 'it_bsik'.

  • lt_fieldcat-ref_fieldname = 'BUKRS'.

ls_layout-zebra = 'X'.

ls_layout-cell_merge = 'X'.

  • **** SORT ****

ls_sort-fieldname = 'LIFNR'.

ls_sort-up = 'X'.

ls_sort-subtot = 'X'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-up = 'X'.

ls_sort-fieldname = 'GJAHR'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-fieldname = 'BUDAT'.

ls_sort-up = 'X'.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_sort-up = 'X'.

ls_sort-fieldname = 'BELNR'.

ls_sort-subtot = ''.

APPEND ls_sort TO lt_sort.

CLEAR ls_sort.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '1'.

ls_fieldcat-outputlen = 20.

ls_fieldcat-seltext_l = 'Document No'.

ls_fieldcat-fieldname = 'BELNR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '2'.

ls_fieldcat-seltext_l = 'Document Date'.

ls_fieldcat-outputlen = 10.

ls_fieldcat-fieldname = 'BUDAT'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '3'.

ls_fieldcat-seltext_l = 'Year'.

ls_fieldcat-fieldname = 'GJAHR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '4'.

ls_fieldcat-seltext_l = 'Vendor Code'.

ls_fieldcat-fieldname = 'LIFNR'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '5'.

ls_fieldcat-seltext_l = 'G/L Account'.

ls_fieldcat-fieldname = 'HKONT'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '6'.

ls_fieldcat-reptext_ddic = 'Amount'.

ls_fieldcat-fieldname = 'WRBTR'.

ls_fieldcat-do_sum = 'X'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '7'.

ls_fieldcat-seltext_l = 'Currency Code'.

ls_fieldcat-outputlen = 10.

ls_fieldcat-fieldname = 'WAERS'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-tabname = 'IT_TEMP'.

ls_fieldcat-col_pos = '8'.

ls_fieldcat-seltext_l = 'Indicator'.

ls_fieldcat-fieldname = 'SHKZG'.

APPEND ls_fieldcat TO lt_fieldcat.

CLEAR ls_fieldcat.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

I_CALLBACK_PF_STATUS_SET = 'SETSTATUS'

I_CALLBACK_USER_COMMAND = 'USRCMD'

IS_LAYOUT = ls_layout

it_fieldcat = lt_fieldcat[]

IT_SORT = lt_sort

I_SAVE = 'A'

TABLES

t_outtab = it_temp[]

  • EXCEPTIONS

  • PROGRAM_ERROR = 1

  • OTHERS = 2

.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

kesavadas_thekkillath
Active Contributor
0 Kudos

report zpp473_vehicle_reversal_rep no standard page heading

message-id zpp.

tables:zpp18_rt_corr.

type-pools:slis.

data:begin of wa_veh_rev,

vehno type zpp18_rt_corr-vehno,

matnr type zpp18_rt_corr-matnr,

werks type zpp18_rt_corr-werks,

mdv01 type zpp18_rt_corr-mdv01,

engno type zpp18_rt_corr-engno,

frmno type zpp18_rt_corr-frmno,

erfmg type zpp18_rt_corr-erfmg,

shift type zpp18_rt_corr-shift,

zuname type zpp18_rt_corr-zuname,

zdate type zpp18_rt_corr-zdate,

maktx type makt-maktx,

zreason_des type zpp17_reason_cod-zreason_des,

*VTEXT TYPE T179T-VTEXT,

end of wa_veh_rev.

data:it_veh_rev like standard table of wa_veh_rev.

data: alvfld type slis_fieldcat_alv,

fieldcat type slis_t_fieldcat_alv.

data: wk_layout type slis_layout_alv,

i_sort type slis_t_sortinfo_alv,

w_sort like line of i_sort.

selection-screen begin of block b1 with frame title text-001.

select-options:

so_zdate for zpp18_rt_corr-zdate obligatory,

so_werks for zpp18_rt_corr-werks,

so_shift for zpp18_rt_corr-shift,

so_mdv01 for zpp18_rt_corr-mdv01,

so_matnr for zpp18_rt_corr-matnr,

so_vehno for zpp18_rt_corr-vehno,

so_engno for zpp18_rt_corr-engno,

so_frmno for zpp18_rt_corr-frmno,

so_uname for zpp18_rt_corr-zuname.

selection-screen end of block b1.

*SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.

*PARAMETERS:CH_MOD AS CHECKBOX.

*SELECTION-SCREEN END OF BLOCK B2.

start-of-selection.

**GET REVERSAL,MATERIAL DESCRIPTIONS, REASON DESCRIPTIONS

select zpp18_rt_corr~vehno

zpp18_rt_corr~matnr

zpp18_rt_corr~werks

zpp18_rt_corr~mdv01

zpp18_rt_corr~engno

zpp18_rt_corr~frmno

zpp18_rt_corr~erfmg

zpp18_rt_corr~shift

zpp18_rt_corr~zuname

zpp18_rt_corr~zdate

makt~maktx

zpp17_reason_cod~zreason_des

into table it_veh_rev from

zpp18_rt_corr inner join makt

on ( zpp18_rt_corrmandt = maktmandt and

zpp18_rt_corrmatnr = maktmatnr )

inner join zpp17_reason_cod

on ( zpp18_rt_corrmandt = zpp17_reason_codmandt and

zpp18_rt_corrzreason = zpp17_reason_codzreason )

client specified

where zpp18_rt_corr~mandt = sy-mandt

and zpp18_rt_corr~zdate in so_zdate

and zpp18_rt_corr~werks in so_werks

and zpp18_rt_corr~shift in so_shift

and zpp18_rt_corr~matnr in so_matnr

and zpp18_rt_corr~mdv01 in so_mdv01

and zpp18_rt_corr~vehno in so_vehno

and zpp18_rt_corr~engno in so_engno

and zpp18_rt_corr~frmno in so_frmno

and zpp18_rt_corr~zuname in so_uname

and makt~spras = sy-langu.

if sy-subrc = 0.

sort it_veh_rev ascending by zdate shift werks mdv01 matnr.

else.

message i961 with 'No Data Found...Try Again !!!'.

stop.

endif.

**BUILD ALV DISPLAY.

define alv_macro.

alvfld-fieldname = &1.

alvfld-seltext_m = &2.

alvfld-col_pos = &3.

if &1 = 'ERFMG'.

alvfld-do_sum = 'X'.

endif.

append alvfld to fieldcat.

clear alvfld.

end-of-definition.

refresh fieldcat.

wk_layout-colwidth_optimize = 'X'.

alv_macro 'ZDATE' 'Rev.Date' '1'.

alv_macro 'SHIFT' 'Shift' '2'.

alv_macro 'WERKS' 'Plant' '3'.

alv_macro 'MDV01' 'Conveyor' '4'.

refresh i_sort.

w_sort-spos = 1.

w_sort-fieldname = 'ZDATE'.

w_sort-up = 'X'.

append w_sort to i_sort.

clear w_sort.

alv_macro 'MATNR' 'Material.No' '5'.

alv_macro 'MAKTX' 'Description' '6'.

w_sort-spos = 6.

w_sort-fieldname = 'MAKTX'.

w_sort-up = 'X'.

w_sort-subtot = 'X'.

append w_sort to i_sort.

clear w_sort.

alv_macro 'VEHNO' 'Veh.No' '7'.

alv_macro 'ENGNO' 'Eng.No' '8'.

alv_macro 'FRMNO' 'Frame.No' '9'.

alv_macro 'ERFMG' 'Qty' '10'.

alv_macro 'ZREASON_DES' 'Reason' '11'.

alv_macro 'ZUNAME' 'User.Reversed' '12'.

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_grid_title = 'Vehicle Reversal Report'

is_layout = wk_layout

it_fieldcat = fieldcat[]

it_sort = i_sort[]

tables

t_outtab = it_veh_rev[]

exceptions

program_error = 1

others = 2.

if sy-subrc <> 0.

message id sy-msgid type sy-msgty number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

end-of-selection.

check this