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: 

Calculate subtotal in ALV

Former Member
0 Kudos

How can i do to calculate several subtotals in an alv?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

in field catalog

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'NETWR'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'NETWR'.

lf_fieldcat-cfieldname = 'WAERS'.

<b> lf_fieldcat-do_sum = 'X'.</b>

APPEND lf_fieldcat TO lt_fieldcat.

You will get subtotal for the field

7 REPLIES 7

Former Member
0 Kudos

in field catalog just add one line...

wa_fldcat-DO_sum = 'X'.

this will display the subtotals of the fields.

0 Kudos

Is there any sample with this?

Thanks

0 Kudos

Hi,

Refer to the following code:

PERFORM f_build_fieldcat USING : 'ITDATA' 'HSL_QUAR01' 'CURR' '15' text-023 text-023 'C' 'X' space.

FORM f_build_fieldcat USING l_tabname TYPE slis_fieldcat_alv-tabname

l_fieldname TYPE slis_fieldcat_alv-fieldname

l_datatype TYPE slis_fieldcat_alv-datatype

l_outputlen TYPE slis_fieldcat_alv-outputlen

l_seltext_l TYPE slis_fieldcat_alv-seltext_l

l_seltext_m TYPE slis_fieldcat_alv-seltext_m

l_just TYPE slis_fieldcat_alv-just

l_do_sum TYPE slis_fieldcat_alv-do_sum

l_no_out TYPE slis_fieldcat_alv-no_out.

CLEAR: ls_fieldcat.

wf_column = wf_column + 1.

ls_fieldcat-tabname = l_tabname.

ls_fieldcat-fieldname = l_fieldname.

ls_fieldcat-datatype = l_datatype.

ls_fieldcat-outputlen = l_outputlen.

ls_fieldcat-seltext_l = l_seltext_l.

ls_fieldcat-seltext_m = l_seltext_m .

ls_fieldcat-just = 'C'.

ls_fieldcat-do_sum = l_do_sum.

ls_fieldcat-no_out = l_no_out.

ls_fieldcat-col_pos = wf_column.

APPEND ls_fieldcat TO int_fieldcat.

CLEAR ls_fieldcat.

ENDFORM. " f_build_fieldcat

Hope this helps.

Reward if helpful.

Regards,

Sipra

Former Member
0 Kudos

hi

good

check this code

go to code wizard and first instantiate the used component and then call the methos in used componenet i.e call the method get_model( ).

after that you write the code below.

...Set Aggregation and Group Aggregation Values

DATA:

lr_field_settings TYPE REF TO if_salv_wd_field_settings.

lr_field_settings ?= l_value.

lr_field_settings->set_group_aggr_displayed( abap_true ).

*...Sort Field Billing Document

DATA:

lr_field TYPE REF TO cl_salv_wd_field,

lr_sort_rule TYPE REF TO cl_salv_wd_sort_rule,

lr_aggr_rule TYPE REF TO cl_salv_wd_aggr_rule.

lr_field_settings ?= l_value.

lr_field = lr_field_settings->get_field( 'BILL_DOC' ).

lr_field->if_salv_wd_sort~set_group_aggregation_allowed( abap_true ).

lr_field->if_salv_wd_sort~create_sort_rule( ).

lr_sort_rule = lr_field->if_salv_wd_sort~get_sort_rule( ).

lr_sort_rule->set_sort_order( if_salv_wd_c_sort=>sort_order_ascending ).

lr_sort_rule->set_group_aggregation( abap_true ).

*...Aggregate Field Amount

lr_field = lr_field_settings->get_field( 'AMOUNT_OUTPUT' ).

lr_field->if_salv_wd_aggr~create_aggr_rule( ).

lr_aggr_rule = lr_field->if_salv_wd_aggr~get_aggr_rule( ).

lr_aggr_rule->set_aggregation_type( if_salv_wd_c_aggregation=>aggrtype_total ).

thanks

mrutyun^

Former Member
0 Kudos

Hi,

Here i am sending following code for ur refference. then u implement for which field u want to sum then change field name u will get out put.

i_fieldcat1-fieldname = 'NTGEW'.

i_fieldcat1-seltext_m = 'NET WEIGHT'.

i_fieldcat1-do_sum = 'X'.

APPEND i_fieldcat1 TO i_fieldcat1.

CLEAR i_fieldcat1.

reward me a points if it is use full answer...

praveen

Former Member
0 Kudos

in field catalog

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'NETWR'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'NETWR'.

lf_fieldcat-cfieldname = 'WAERS'.

<b> lf_fieldcat-do_sum = 'X'.</b>

APPEND lf_fieldcat TO lt_fieldcat.

You will get subtotal for the field

Former Member
0 Kudos

Sample code

&----


*& TABLES DECLARATION *

&----


TABLES : ekko, ekpo, t001w, t161t, lfa1.

&----


*& INTERNAL TABLE DECLARATION *

&----


TYPES : BEGIN OF tp_itab1,

bukrs TYPE ekko-bukrs,

lifnr TYPE ekko-lifnr,

ebeln TYPE ekko-ebeln,

waers TYPE ekko-waers,

bsart TYPE ekko-bsart,

ekorg TYPE ekko-ekorg,

ekgrp TYPE ekko-ekgrp,

ebelp TYPE ekpo-ebelp,

txz01 TYPE ekpo-txz01,

matnr TYPE ekpo-matnr,

werks TYPE ekpo-werks,

menge TYPE ekpo-menge,

meins TYPE ekpo-meins,

netpr TYPE ekpo-netpr,

netwr TYPE ekpo-netwr,

name1 TYPE t001w-name1,

header_text(132),

chk TYPE c,

END OF tp_itab1.

DATA : t_itab1 TYPE STANDARD TABLE OF tp_itab1,

wa_itab1 TYPE tp_itab1,

ws_name1 TYPE t001w-name1.

DATA : f_itab1 TYPE STANDARD TABLE OF tp_itab1,

wa_fitab1 TYPE tp_itab1.

DATA : f_itab2 TYPE STANDARD TABLE OF tp_itab1,

wa_fitab2 TYPE tp_itab1.

TYPES : BEGIN OF tp_name1,

werks TYPE t001w-werks,

name1 TYPE t001w-name1,

END OF tp_name1.

DATA : t_name1 TYPE STANDARD TABLE OF tp_name1,

wa_name1 TYPE tp_name1.

DATA : t_lfa1 TYPE STANDARD TABLE OF lfa1,

wa_lfa1 TYPE lfa1.

DATA: hold_tabix TYPE sy-tabix.

DATA : t_tlinetab TYPE STANDARD TABLE OF tline,

wa_tlinetab TYPE tline.

DATA: ws_name TYPE thead-tdname,

ws_repid TYPE sy-repid VALUE 'YKTEST'.

DATA : repid TYPE sy-repid.

DATA : total TYPE p DECIMALS 2,

quantity TYPE p DECIMALS 3.

CONSTANTS : ws_id TYPE thead-tdid VALUE 'F01',

ws_object TYPE thead-tdobject VALUE 'EKKO',

lc_view(1) TYPE c VALUE 'F',

lc_form(10) TYPE c VALUE 'YK_FORM'.

&----


*& ALV DECLARATION *

&----


TYPE-POOLS : slis.

DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,

lf_fieldcat TYPE slis_fieldcat_alv.

DATA: lh_index LIKE lf_fieldcat-col_pos.

DATA: l_layout TYPE slis_layout_alv.

DATA : l_sort TYPE slis_t_sortinfo_alv,

w_sort TYPE slis_sortinfo_alv.

&----


*& SELECTION-SCREEN *

&----


SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE text-001.

SELECTION-SCREEN SKIP 2.

SELECT-OPTIONS: s_bukrs FOR ekko-bukrs OBLIGATORY.

SELECT-OPTIONS: s_lifnr FOR ekko-lifnr OBLIGATORY.

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

PARAMETERS: p_bsart TYPE ekko-bsart.

SELECT-OPTIONS: s_matnr FOR ekpo-matnr.

SELECT-OPTIONS: s_ekorg FOR ekko-ekorg.

SELECT-OPTIONS: s_ekgrp FOR ekko-ekgrp.

SELECTION-SCREEN SKIP 2.

SELECTION-SCREEN END OF BLOCK a1.

&----


*& START-OF-SELECTION *

&----


START-OF-SELECTION.

  • FETCHING DATA

PERFORM fetch_data.

  • BUILD FIELD CATALOG

PERFORM build_fieldcatalog.

  • BUILD LAYOUT

PERFORM build_layout.

  • SORT LAYOUT

PERFORM sort_layout.

&----


*& END-OF-SELECTION. *

&----


END-OF-SELECTION.

*DISPLAY GRID

PERFORM display_grid.

&----


*& Form fetch_data *

&----


FORM fetch_data.

IF p_bsart IS INITIAL.

SELECT ekko~bukrs

ekko~lifnr

ekko~ebeln

ekko~waers

ekko~bsart

ekko~ekorg

ekko~ekgrp

ekpo~ebelp

ekpo~txz01

ekpo~matnr

ekpo~werks

ekpo~menge

ekpo~meins

ekpo~netpr

ekpo~netwr

INTO TABLE t_itab1 FROM

ekko INNER JOIN ekpo ON ekkoebeln = ekpoebeln

WHERE ekko~ebeln IN s_ebeln AND

ekko~bukrs IN s_bukrs AND

ekko~lifnr IN s_lifnr AND

ekko~ekorg IN s_ekorg AND

ekko~ekgrp IN s_ekgrp AND

ekpo~matnr IN s_matnr.

ELSE.

SELECT ekko~bukrs

ekko~lifnr

ekko~ebeln

ekko~waers

ekko~bsart

ekko~ekorg

ekko~ekgrp

ekpo~ebelp

ekpo~txz01

ekpo~matnr

ekpo~werks

ekpo~menge

ekpo~meins

ekpo~netpr

ekpo~netwr

INTO TABLE t_itab1 FROM

ekko INNER JOIN ekpo ON ekkoebeln = ekpoebeln

WHERE ekko~ebeln IN s_ebeln AND

ekko~bukrs IN s_bukrs AND

ekko~lifnr IN s_lifnr AND

ekko~ekorg IN s_ekorg AND

ekko~ekgrp IN s_ekgrp AND

ekpo~matnr IN s_matnr AND

ekko~bsart = p_bsart.

ENDIF.

IF NOT t_itab1[] IS INITIAL.

  • FETCHING NAME1

SELECT werks

name1

FROM t001w

INTO TABLE t_name1

FOR ALL ENTRIES IN t_itab1

WHERE werks = t_itab1-werks.

SORT t_itab1 BY werks.

SORT t_name1 BY werks.

IF sy-subrc = 0.

LOOP AT t_itab1 INTO wa_itab1.

hold_tabix = sy-tabix.

READ TABLE t_name1 INTO wa_name1 WITH KEY werks = wa_itab1-werks

BINARY SEARCH.

IF sy-subrc = 0.

wa_itab1-name1 = wa_name1-name1.

MODIFY t_itab1 INDEX hold_tabix FROM wa_itab1.

ENDIF.

  • FETCHING HEADER TEXT

MOVE wa_itab1-ebeln TO ws_name.

CALL FUNCTION 'READ_TEXT'

EXPORTING

id = ws_id

language = sy-langu

name = ws_name

object = ws_object

TABLES

lines = t_tlinetab

EXCEPTIONS

id = 1

language = 2

name = 3

not_found = 4

object = 5

reference_check = 6

wrong_access_to_archive = 7

OTHERS = 8.

IF sy-subrc = 0.

LOOP AT t_tlinetab INTO wa_tlinetab.

wa_itab1-header_text = wa_tlinetab-tdline.

MODIFY t_itab1 FROM wa_itab1.

ENDLOOP.

ENDIF. " IF sy-subrc = 0.

CLEAR : wa_itab1,

hold_tabix,

wa_name1.

ENDLOOP.

ENDIF.

ENDIF. " IF NOT t_itab1[] IS INITIAL.

IF t_itab1[] IS INITIAL.

MESSAGE i000(zg) WITH text-008.

ENDIF.

SORT : t_itab1 BY bukrs lifnr ebeln ebelp.

ENDFORM. " fetch_data

&----


*& Form build_fieldcatalog *

&----


FORM build_fieldcatalog .

  • COMPANY CODE

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'BUKRS'.

lf_fieldcat-ref_tabname = 'EKKO'.

lf_fieldcat-ref_fieldname = 'BUKRS'.

lf_fieldcat-fix_column = 'X'.

APPEND lf_fieldcat TO lt_fieldcat.

  • VENDOR

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'LIFNR'.

lf_fieldcat-ref_tabname = 'EKKO'.

lf_fieldcat-ref_fieldname = 'LIFNR'.

lf_fieldcat-fix_column = 'X'.

APPEND lf_fieldcat TO lt_fieldcat.

  • PURCHASING DOCUMENT NUMBER

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'EBELN'.

lf_fieldcat-ref_tabname = 'EKKO'.

lf_fieldcat-ref_fieldname = 'EBELN'.

  • lf_fieldcat-key = 'X'.

lf_fieldcat-do_sum = 'X'.

lf_fieldcat-fix_column = 'X'.

APPEND lf_fieldcat TO lt_fieldcat.

  • ITEM NUMBER OF PURCHASING DOCUMENT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'EBELP'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'EBELP'.

APPEND lf_fieldcat TO lt_fieldcat.

  • SHORT TEXT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'TXZ01'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'TXZ01'.

APPEND lf_fieldcat TO lt_fieldcat.

  • MATERIAL NUMBER

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'MATNR'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'MATNR'.

APPEND lf_fieldcat TO lt_fieldcat.

  • PLANT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'WERKS'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'WERKS'.

APPEND lf_fieldcat TO lt_fieldcat.

  • NAME

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'NAME1'.

lf_fieldcat-ref_tabname = 'T001W'.

lf_fieldcat-ref_fieldname = 'NAME1'.

APPEND lf_fieldcat TO lt_fieldcat.

  • PURCHASE ORDER QUANTITY

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'MENGE'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'MENGE'.

lf_fieldcat-do_sum = 'X'.

lf_fieldcat-qfieldname = 'MEINS'.

APPEND lf_fieldcat TO lt_fieldcat.

  • UNIT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'MEINS'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'MEINS'.

APPEND lf_fieldcat TO lt_fieldcat.

  • NET PRICE IN PURCHASING DOCUMENT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'NETPR'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'NETPR'.

lf_fieldcat-cfieldname = 'WAERS'.

lf_fieldcat-do_sum = 'X'.

APPEND lf_fieldcat TO lt_fieldcat.

  • NET ORDER VALUE IN PO CURRENCY

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'NETWR'.

lf_fieldcat-ref_tabname = 'EKPO'.

lf_fieldcat-ref_fieldname = 'NETWR'.

lf_fieldcat-cfieldname = 'WAERS'.

lf_fieldcat-do_sum = 'X'.

APPEND lf_fieldcat TO lt_fieldcat.

  • CURRENCY

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'WAERS'.

lf_fieldcat-ref_tabname = 'EKKO'.

lf_fieldcat-ref_fieldname = 'WAERS'.

APPEND lf_fieldcat TO lt_fieldcat.

  • HEADER TEXT

CLEAR lf_fieldcat.

lf_fieldcat-fieldname = 'HEADER_TEXT'.

lf_fieldcat-seltext_m = 'Header Text'.

APPEND lf_fieldcat TO lt_fieldcat.

ENDFORM. " build_fieldcatalog

&----


*& Form display_grid *

&----


FORM display_grid.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = ws_repid

i_callback_pf_status_set = 'PF_STATUS'

i_callback_user_command = 'USER_COMMAND'

is_layout = l_layout

it_fieldcat = lt_fieldcat

it_sort = l_sort

i_save = 'X'

  • IMPORTING

TABLES

t_outtab = t_itab1

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.

ENDFORM. " display_grid

&----


*& Form build_layout *

&----


FORM build_layout .

l_layout-colwidth_optimize = 'X'.

l_layout-zebra = 'X'.

l_layout-box_fieldname = 'CHK'.

l_layout-box_tabname = 'T_ITAB1' .

ENDFORM. " build_layout

&----


*& Form sort_layout *

&----


FORM sort_layout .

CLEAR w_sort.

w_sort-fieldname = 'BUKRS'.

w_sort-tabname = 'T_ITAB1'.

w_sort-spos = 1.

w_sort-up = 'X'.

w_sort-subtot = 'X'.

APPEND w_sort TO l_sort.

CLEAR w_sort.

w_sort-fieldname = 'EBELN'.

w_sort-tabname = 'T_ITAB1'.

w_sort-spos = 1.

w_sort-up = 'X'.

w_sort-subtot = 'X'.

APPEND w_sort TO l_sort.

ENDFORM. " sort_layout

&----


*& Form PF_STATUS

&----


FORM pf_status USING rt_extab TYPE slis_t_extab.

SET PF-STATUS 'STANDARD1' EXCLUDING rt_extab.

ENDFORM. " PF_STATUS

&----


*& Form user_command

&----


FORM user_command USING s_ucomm TYPE sy-ucomm

rs_selfield TYPE slis_selfield.

CLEAR wa_itab1.

READ TABLE t_itab1 INTO wa_itab1 INDEX rs_selfield-tabindex.

CASE s_ucomm.

WHEN 'POD'.

SET PARAMETER ID 'BES' FIELD wa_itab1-ebeln.

CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.

CLEAR wa_itab1.

WHEN 'MAT'.

SET PARAMETER ID 'MAT' FIELD wa_itab1-matnr.

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.

WHEN '&IC1'.

IF rs_selfield-fieldname = 'MATNR'.

SET PARAMETER ID 'MAT' FIELD wa_itab1-matnr.

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.

ELSEIF rs_selfield-fieldname = 'EBELN'.

SET PARAMETER ID 'BES' FIELD wa_itab1-ebeln.

CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.

ENDIF.