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: 

Sub total in alv grid

Former Member
0 Kudos
103

Hi All,

I need subtotals against to matnr and matkl.I have 5 fields in my table matnr maktx matkl quant1 quant2.I want to show subtotals against MATKL as well as for the materials?what exactly we have to do? any thing we have to take of position of the fields in the table?since I am getting subtot against only Matnr not with Matkl this is the problem and also I want to see only subtotals in the report output.

Thanks&Regards

Mahesh

6 REPLIES 6

Former Member
0 Kudos
79

Hi mahesh,

Check this code and modify it according to your requirement.

&----


*& Report YMS_SUBHEADINGALV2 *

*& *

&----


*& *

*& *

&----


REPORT YMS_SUBHEADINGALV2.

TYPE-POOLS : SLIS.

TYPES : BEGIN OF YVBRK_LIST,

VBELN LIKE VBRK-VBELN,

FKART LIKE VBRK-FKART,

BELNR LIKE VBRK-BELNR,

END OF YVBRK_LIST.

TYPES : BEGIN OF YVBRP_LIST,

VBELN LIKE VBRP-VBELN,

POSNR LIKE VBRP-POSNR,

NETWR LIKE VBRP-NETWR,

END OF YVBRP_LIST.

DATA: I_VBRK TYPE STANDARD TABLE OF YVBRK_LIST

INITIAL SIZE 0 WITH HEADER LINE.

DATA: I_VBRP TYPE STANDARD TABLE OF YVBRP_LIST

INITIAL SIZE 0 WITH HEADER LINE.

DATA: KEY_INFO TYPE SLIS_KEYINFO_ALV,

REPORT_ID LIKE SY-REPID,

TABLE_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,

GT_EVENTS TYPE SLIS_T_EVENT,

GT_SORT TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,

GS_LAYOUT TYPE SLIS_LAYOUT_ALV,

GT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,

G_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE',

G_TOP_OF_LIST TYPE SLIS_FORMNAME VALUE 'TOP_OF_LIST'.

INITIALIZATION.

REPORT_ID = SY-REPID.

KEY_INFO-HEADER01 = 'VBELN'.

KEY_INFO-ITEM01 = 'VBELN'.

KEY_INFO-HEADER02 = SPACE.

KEY_INFO-ITEM02 = 'NETWR'.

GS_LAYOUT-GROUP_CHANGE_EDIT = 'X'.

GT_SORT-FIELDNAME = 'VBELN'.

GT_SORT-TABNAME = '1'.

GT_SORT-SPOS = 1.

GT_SORT-UP = 'X'.

GT_SORT-SUBTOT = 'X'.

GT_SORT-GROUP = '*'.

APPEND GT_SORT.

GT_SORT-FIELDNAME = 'POSNR'.

GT_SORT-TABNAME = '2'.

GT_SORT-SPOS = 2.

GT_SORT-UP = 'X'.

GT_SORT-SUBTOT = 'X'.

GT_SORT-GROUP = ''.

APPEND GT_SORT.

TABLE_FIELDCAT-FIELDNAME = 'VBELN'.

TABLE_FIELDCAT-seltext_m = 'Sales Document'.

TABLE_FIELDCAT-TABNAME = '1'.

TABLE_FIELDCAT-COL_POS = '1'.

TABLE_FIELDCAT-OUTPUTLEN = 17.

TABLE_FIELDCAT-DO_SUM = ' '.

APPEND TABLE_FIELDCAT.

TABLE_FIELDCAT-FIELDNAME = 'POSNR'.

TABLE_FIELDCAT-seltext_m = 'Sales Document Item'.

TABLE_FIELDCAT-TABNAME = '2'.

TABLE_FIELDCAT-COL_POS = '1'.

TABLE_FIELDCAT-OUTPUTLEN = 14.

TABLE_FIELDCAT-DO_SUM = ' '.

APPEND TABLE_FIELDCAT.

TABLE_FIELDCAT-FIELDNAME = 'NETWR'.

TABLE_FIELDCAT-seltext_m = 'Net value'.

TABLE_FIELDCAT-TABNAME = '2'.

TABLE_FIELDCAT-COL_POS = '2'.

TABLE_FIELDCAT-OUTPUTLEN = 15.

TABLE_FIELDCAT-DO_SUM = 'X'.

APPEND TABLE_FIELDCAT.

PERFORM EVENTTAB_BUILD USING GT_EVENTS[].

START-OF-SELECTION.

SELECT VBELN

FKART

BELNR

INTO CORRESPONDING FIELDS OF TABLE I_VBRK

FROM VBRK

WHERE VBELN > '0090000121' AND VBELN < '0090000135'.

SELECT VBELN

POSNR

NETWR

INTO CORRESPONDING FIELDS OF TABLE I_VBRP

FROM VBRP

FOR ALL ENTRIES IN I_VBRK

WHERE VBELN = I_VBRK-VBELN.

PERFORM WRITE_REPORT.

*&----


*& Form EVENTTAB_BUILD

*&----


  • text

*----


  • -->RT_EVENTS text

*----


FORM EVENTTAB_BUILD USING RT_EVENTS TYPE SLIS_T_EVENT.

"Registration of events to happen during list display

DATA: LS_EVENT TYPE SLIS_ALV_EVENT.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

I_LIST_TYPE = 0

IMPORTING

ET_EVENTS = RT_EVENTS.

READ TABLE RT_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE

INTO LS_EVENT.

IF SY-SUBRC = 0.

MOVE G_TOP_OF_PAGE TO LS_EVENT-FORM.

APPEND LS_EVENT TO RT_EVENTS.

ENDIF.

ENDFORM. "EVENTTAB_BUILD

*&----


*& Form TOP_OF_PAGE

*&----


  • text

*----


FORM TOP_OF_PAGE.

DATA: LS_LINE TYPE SLIS_LISTHEADER.

  • Header Data

REFRESH GT_LIST_TOP_OF_PAGE.

CLEAR LS_LINE.

  • LS_LINE-TYP = 'H'.

  • LS_LINE-INFO = 'Hierarchical Lists'.

APPEND LS_LINE TO GT_LIST_TOP_OF_PAGE.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

IT_LIST_COMMENTARY = GT_LIST_TOP_OF_PAGE.

ENDFORM. "TOP_OF_PAGE

*&----


*& Form write_report

*&----


  • text

*----


FORM WRITE_REPORT.

CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = REPORT_ID

IS_LAYOUT = GS_LAYOUT

IT_FIELDCAT = TABLE_FIELDCAT[]

IT_SORT = GT_SORT[]

IT_EVENTS = GT_EVENTS[]

I_TABNAME_HEADER = '1'

I_TABNAME_ITEM = '2'

I_STRUCTURE_NAME_HEADER = 'YVBRK_LIST'

I_STRUCTURE_NAME_ITEM = 'YVBRP_LIST'

IS_KEYINFO = KEY_INFO

TABLES

T_OUTTAB_HEADER = I_VBRK

T_OUTTAB_ITEM = I_VBRP.

ENDFORM. "write_report

Hope this resolves your query.

Reward all the helpful answers.

Regards

Former Member
0 Kudos
79

Hi,

The best way would be to sort the internal table based on Matkl and matnr ( make them 1st and 2nd columns) and calculate the subtotals.

Have the subtotals in another internal table as you want to display only the subtotals in the ALV Grid.

Use the subtotals internal table for the ALV grid.

Hope this helps,

Thanks,

Rashmi.

Former Member
0 Kudos
79

REPORT z_example.

TYPE-POOLS: slis.

DATA: BEGIN OF it_output OCCURS 0,

var1(8) TYPE n,

var2(10),

var3 TYPE I,

END OF it_output.

DATA: it_fieldcat TYPE slis_t_fieldcat_alv,

t_fieldcat TYPE slis_fieldcat_alv,

it_sort TYPE slis_t_sortinfo_alv,

t_sort TYPE slis_sortinfo_alv,

v_repid LIKE sy-repid.

INITIALIZATION.

v_repid = sy-repid.

START-OF-SELECTION.

PERFORM get_data.

PERFORM sort_fields.

PERFORM fill_fieldcat.

PERFORM list_display.

&----


*& Form GET_DATA

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_data.

it_output-var1 = 1000.

it_output-var2 = 'anupama'.

it_output-var3 = '10000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 1000.

it_output-var2 = 'siddhu'.

it_output-var3 = '20000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 1000.

it_output-var2 = 'chinni'.

it_output-var3 = '100000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 2000.

it_output-var2 = 'chicchu'.

it_output-var3 = '10000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 2000.

it_output-var2 = 'candy'.

it_output-var3 = '10000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 1000.

it_output-var2 = 'anupama'.

it_output-var3 = '10000'.

APPEND it_output.

CLEAR it_output.

it_output-var1 = 4000.

it_output-var2 = 'anupama'.

it_output-var3 = '10000'.

APPEND it_output.

CLEAR it_output.

ENDFORM. " GET_DATA

&----


*& Form fill_fieldcat

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM fill_fieldcat.

PERFORM fill_fields USING: 'IT_OUTPUT' 'VAR1' 'Variable 1' ' ',

'IT_OUTPUT' 'VAR2' 'Variable 2' ' ',

'IT_OUTPUT' 'VAR3' 'Variable 3' 'X'.

ENDFORM. " fill_fieldcat

&----


*& Form fill_fields

&----


  • text

----


  • -->P_0146 text

  • -->P_0147 text

  • -->P_0148 text

  • -->P_0149 text

----


FORM fill_fields USING value(tabname) TYPE slis_tabname

value(fieldname) TYPE slis_fieldname

value(seltext_m) LIKE dd03p-scrtext_m

value(do_sum) TYPE c.

t_fieldcat-tabname = tabname.

t_fieldcat-fieldname = fieldname.

t_fieldcat-seltext_m = seltext_m.

IF do_sum = 'X'.

t_fieldcat-datatype = 'CURR'.

ENDIF.

t_fieldcat-do_sum = do_sum.

APPEND t_fieldcat TO it_fieldcat.

CLEAR t_fieldcat.

ENDFORM. " fill_fields

&----


*& Form list_display

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM list_display.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

i_callback_program = v_repid

it_fieldcat = it_fieldcat

it_sort = it_sort[]

TABLES

t_outtab = it_output

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. " list_display

&----


*& Form sort_fields

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM sort_fields.

t_sort-fieldname = 'VAR1'.

t_sort-tabname = 'IT_OUTPUT'.

t_sort-spos = 1.

t_sort-up = 'X'.

t_sort-subtot = 'X'.

APPEND t_sort TO it_sort.

CLEAR t_sort.

t_sort-fieldname = 'VAR3'.

t_sort-tabname = 'IT_OUTPUT'.

t_sort-spos = 2.

t_sort-up = 'X'.

APPEND t_sort TO it_sort.

CLEAR t_sort.

ENDFORM.

or see this also

Refer below demo code and see perform Sort_list..

it wil serve ur purpose.

REPORT ZGILL_ALV message-id rp .

type-pools slis.

tables: zgill_main,zgill_details.

data z_fieldcat type slis_t_fieldcat_alv.

data begin of itab occurs 0.

DATA ICON TYPE ICON-ID.

include structure zgill_main.

data salary like zgill_details-salary.

data end of itab.

*data itab1 like table of itab with header line.

data : WA_SORT TYPE SLIS_SORTINFO_ALV,

IT_SORT TYPE SLIS_T_SORTINFO_ALV,

WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV,

IT_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV,

WA_LAYOUT TYPE SLIS_LAYOUT_ALV.

PARAMETERS: p_list radiobutton group A1,

P_GRID RADIOBUTTON GROUP A1.

SELECT-OPTIONS: S_PERNR FOR ZGILL_MAIN-PERNR.

start-of-selection.

perform fill_itab.

perform sort_list.

**************Start of scenario without container******************************************

*********Method 1***********

perform fill_fieldcat. " Manuallly Preparing Fiedl Catalog

******************************

*********Method 2***********

*perform fill_fieldcat1 changing z_fieldcat. "Preparing field catalog with merge function

******************************

perform display_alv.

*****************end of scenario without container*****************************************

&----


*& Form fill_itab

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form fill_itab .

*select * from zgill_main up to 20 rows INTO CORRESPONDING FIELDS OF TABLE itab.

*ITAB1[] = ITAB[].

select apernr aname aorg adob b~salary INTO CORRESPONDING FIELDS OF TABLE itab

from zgill_main as a join zgill_details as b on apernr = bpernr

WHERE A~PERNR IN S_PERNR.

LOOP AT ITAB.

IF ITAB-PERNR < 1111.

ITAB-ICON = '@08@'.

ELSEIF ITAB-PERNR > 1111 AND ITAB-PERNR < 11111111.

ITAB-ICON = '@09@'.

ELSEIF ITAB-PERNR GE 11111111.

ITAB-ICON = '@0A@'.

ENDIF.

MODIFY ITAB INDEX SY-TABIX.

ENDLOOP.

endform. " fill_itab

&----


*& Form display_alv

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form display_alv .

data repid like sy-repid.

REPID = SY-REPID.

WA_LAYOUT-ZEBRA = 'X'.

WA_LAYOUT-WINDOW_TITLEBAR = 'GRID DISPLAY'.

WA_LAYOUT-SUBTOTALS_TEXT = 'SUBTOTAL'.

WA_LAYOUT-TOTALS_TEXT = 'TOTAL'.

IF P_GRID = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = repid

IT_FIELDCAT = IT_FIELDTAB

IT_SORT = IT_SORT

IS_LAYOUT = WA_LAYOUT

TABLES

t_outtab = itab[]

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

.

IF sy-subrc <> 0.

message e016 with 'Error in Display'.

ENDIF.

ELSEIF P_LIST = 'X'.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = repid

IT_FIELDCAT = IT_FIELDTAB

IT_SORT = IT_SORT

IS_LAYOUT = WA_LAYOUT

TABLES

t_outtab = itab[]

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

IF sy-subrc <> 0.

message e016 with 'Error in Display'.

ENDIF.

ENDIF.

endform. " display_alv

&----


*& Form fill_fieldcat1

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form fill_fieldcat1 changing d_fcat type slis_t_fieldcat_alv.

data repid like sy-repid.

data d_fcat1 type slis_t_fieldcat_alv with header line.

REPID = SY-REPID.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = repid

I_INTERNAL_TABNAME = 'ITAB'

I_STRUCTURE_NAME = 'ZGILL_MAIN'

  • I_CLIENT_NEVER_DISPLAY = 'X'

  • I_INCLNAME =

  • I_BYPASSING_BUFFER =

  • I_BUFFER_ACTIVE =

CHANGING

ct_fieldcat = d_fcat[]

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

IF sy-subrc <> 0.

message e016 with 'Error in preparing fiedl catalog'.

ENDIF.

loop at d_fcat into d_fcat1.

case d_fcat1-fieldname.

when 'NAME'.

d_fcat1-reptext_ddic = 'Emp Name'.

MODIFY D_FCAT FROM D_FCAT1.

WHEN 'PERNR'.

d_fcat1-reptext_ddic = 'Emp Num'.

MODIFY D_FCAT FROM D_FCAT1.

WHEN 'ORG'.

d_fcat1-reptext_ddic = 'Org Unit'.

MODIFY D_FCAT FROM D_FCAT1.

endcase.

clear d_fcat1.

endloop.

endform. " fill_fieldcat1

&----


*& Form sort_list

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form sort_list .

CLEAR WA_SORT.

WA_SORT-FIELDNAME = 'DOB'.

WA_SORT-SPOS = '1'.

WA_SORT-UP = 'X'.

WA_SORT-SUBTOT = 'X'.

APPEND WA_SORT TO IT_SORT.

CLEAR WA_SORT.

WA_SORT-FIELDNAME = 'NAME'.

WA_SORT-SPOS = '1'.

WA_SORT-UP = 'X'.

APPEND WA_SORT TO IT_SORT.

CLEAR WA_SORT.

endform. " sort_list

&----


*& Form fill_fieldcat

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form fill_fieldcat .

CLEAR WA_FIELDCAT.

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'ICON'.

WA_FIELDCAT-SELTEXT_L = 'TRAFFIC'.

WA_FIELDCAT-ICON = 'X'.

  • WA_FIELDCAT-ddictxt = 'l'.

WA_FIELDCAT-COL_POS = 1.

WA_FIELDCAT-OUTPUTLEN = 10.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

CLEAR WA_FIELDCAT .

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'PERNR'.

WA_FIELDCAT-SELTEXT_L = 'EMP NUMBER'.

  • WA_FIELDCAT-ddictxt = 'l'.

WA_FIELDCAT-COL_POS = 2.

WA_FIELDCAT-OUTPUTLEN = 10.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

CLEAR WA_FIELDCAT .

  • when 'maktx'.

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'NAME'.

WA_FIELDCAT-SELTEXT_L = 'EMP NAME'.

  • WA_FIELDCAT-ddictxt = 'l'.

WA_FIELDCAT-COL_POS = 3.

WA_FIELDCAT-OUTPUTLEN = 15.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

CLEAR WA_FIELDCAT .

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'ORG'.

WA_FIELDCAT-SELTEXT_L = 'ORG UNIT'.

WA_FIELDCAT-COL_POS = 4.

WA_FIELDCAT-OUTPUTLEN = 10.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

CLEAR WA_FIELDCAT .

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'DOB'.

WA_FIELDCAT-SELTEXT_L = 'BIRTH DATE'.

WA_FIELDCAT-COL_POS = 5.

WA_FIELDCAT-OUTPUTLEN = 12.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

CLEAR WA_FIELDCAT .

WA_FIELDCAT-TABNAME = 'ITAB'.

WA_FIELDCAT-FIELDNAME = 'SALARY'.

WA_FIELDCAT-SELTEXT_L = 'SALARY'.

WA_FIELDCAT-COL_POS = 6.

WA_FIELDCAT-OUTPUTLEN = 25.

WA_FIELDCAT-do_sum = 'X'.

APPEND WA_FIELDCAT TO IT_FIELDTAB.

endform. " fill_fieldcat

reward if helps

Former Member
0 Kudos
79

Shift MATKL as the second field field in ur internal table and populate MATNR and MATKL in the sort table so that you get subtotals on those 2 fields

it_sort-fieldname = 'MATNR'.
it_sort-subtot = 'X'.
it_sort-spos = '1'.
append it_sort.

it_sort-fieldname = 'MATKL'.
it_sort-subtot = 'X'.
it_sort-spos = '2'.
append it_sort.

Message was edited by:

Chandrasekhar Jagarlamudi

Former Member

0 Kudos
79

Hi All still I am not getting this displaying only subtotals in the report output.

I have 6 fields in my table i.e Matkl matnr arktx quan1 quan2 quan3. I need to display subtotals against to Matkl but the output should display only this subtotals only not all the items.I passed LAYOUT-total_only = 'X' and for sort int table i passed EXPA = 'X' still it's not coming correctly can somebody suggest me regarding this?

Thanks&Regards

Mahesh