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 of a particular column after sorting in ALV grid

Former Member
0 Kudos

Hi all,

I need to get a subtotal of a given Column ....I can do it through Clicking on button of Subtotal.

I want the same to be written in my Program.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

For subtotals in alv, you ned to sort the table and you need to pass this table into relative function module say FM Reuse_alv_list_display or Reuse_alv_list_grid_display

See the below sample code

add 1 to ls_sort-spos.

ls_sort-fieldname = 'which field you want todo the sum

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

Finally pass in the below function module

Call the below Function module and pass the sort internal table

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = ls_layout

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = gt_vbak.

Regards

Ramakrishna Pathi

7 REPLIES 7

former_member181995
Active Contributor
0 Kudos

Take a look Sample Code:

CLEAR gs1_sort.
gs1_sort-fieldname = 'HSORTKEY'.
  gs1_sort-spos      = 1.
  gs1_sort-up        = 'X'.
  gs1_sort-subtot    = 'X'.
  APPEND gs1_sort TO gt1_sort.

  CLEAR gs1_sort.
  gs1_sort-fieldname = 'SORTKEY'.
  gs1_sort-spos      = 2.
  gs1_sort-up        = 'X'.
  gs1_sort-subtot    = 'X'.
  APPEND gs1_sort TO gt1_sort.

  CLEAR gs1_sort.
  gs1_sort-fieldname = 'PRCTR'.
  gs1_sort-spos      = 3.
  gs1_sort-up        = 'X'.
*gs1_sort-SUBTOT    = 'X'.
  APPEND gs1_sort TO gt1_sort.

  CLEAR gs1_sort.
  gs1_sort-fieldname = 'ACCNTN'.
  gs1_sort-spos      = 4.
  gs1_sort-up        = 'X'.
*gs1_sort-SUBTOT    = 'X'.
  APPEND gs1_sort TO gt1_sort.

where gs1_sort has to be call in Reuse_ALV FM with IT_SORT table.

Now can you tell me did you search in SCN?If yes what was the search results?

Cheers,

Amit.

0 Kudos

Means?

0 Kudos

please search for the same before posting

you can do like this...

the total program is here .....

REPORT z_demo_alv_sort.

TABLES : vbak.

TYPE-POOLS: slis. " ALV Global types

SELECT-OPTIONS :

s_vkorg FOR vbak-vkorg, " Sales organization

s_kunnr FOR vbak-kunnr, " Sold-to party

s_vbeln FOR vbak-vbeln. " Sales document

SELECTION-SCREEN :

SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max.

PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.

SELECTION-SCREEN END OF LINE.

DATA:

BEGIN OF gt_vbak OCCURS 0,

vkorg LIKE vbak-vkorg, " Sales organization

kunnr LIKE vbak-kunnr, " Sold-to party

vbeln LIKE vbak-vbeln, " Sales document

netwr LIKE vbak-netwr, " Net Value of the Sales Order

waerk LIKE vbak-waerk, " Document currency

END OF gt_vbak.

INITIALIZATION.

v_1 = 'Maximum of records to read'.

START-OF-SELECTION.

PERFORM f_read_data.

PERFORM f_display_data.

-


Form f_read_data

-


FORM f_read_data.

SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbak

FROM vbak

UP TO p_max ROWS

WHERE kunnr IN s_kunnr

AND vbeln IN s_vbeln

AND vkorg IN s_vkorg.

ENDFORM. " F_READ_DATA

-


Form f_display_data

-


FORM f_display_data.

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-ref_tabname = 'VBAK'.

ls_fieldcat-do_sum = &2.

ls_fieldcat-cfieldname = &3.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

END-OF-DEFINITION.

DATA:

ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

m_fieldcat 'VKORG' '' ''.

m_fieldcat 'KUNNR' '' ''.

m_fieldcat 'VBELN' '' ''.

m_fieldcat 'NETWR' 'X' 'WAERK'.

m_fieldcat 'WAERK' '' ''.

m_sort 'VKORG' 'X'. " Sort by vkorg and subtotal

m_sort 'KUNNR' 'X'. " Sort by kunnr and subtotal

m_sort 'VBELN' ''. " Sort by vbeln

ls_layout-cell_merge = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = ls_layout

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = gt_vbak.

ENDFORM. " F_DISPLAY_DATA

0 Kudos

I searched but didn't got any ...

Former Member
0 Kudos

Hi,

Create an internal table of type slis_sortinfo_alv and a work area say wa.

wa-spos = 1.

wa-fieldname = <name of the field on which subtotal>

wa-tabname = it (where it is the internal table which has the field)

wa-subtot = 'X'.

append wa to it_sort.

it_sort is the internal table of type slis_sortinfo_alv.

Now pass this internal table to the REUSE_ALV_GRID_DISPALY to the parameter IT_SORT.

Say EBELN (PO number is on which sub totalling is required)

sort itab by ebeln.

wa_sort-spos = 1.

wa_sort-fieldname = 'EBELN'.

wa_sort-tabname = 'ITAB'.

wa_sort-subtot = 'X'.

APPEND wa_sort TO it_sort.

now pass it_sort to parameter it_sort od function module REUSE_ALV_GRID_DISPLAY

Hope this helps.

Regards,

Sachin

Edited by: Sachin Dargan on Mar 9, 2009 12:59 PM

Former Member
0 Kudos

Hi,

For subtotals in alv, you ned to sort the table and you need to pass this table into relative function module say FM Reuse_alv_list_display or Reuse_alv_list_grid_display

See the below sample code

add 1 to ls_sort-spos.

ls_sort-fieldname = 'which field you want todo the sum

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

Finally pass in the below function module

Call the below Function module and pass the sort internal table

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = ls_layout

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = gt_vbak.

Regards

Ramakrishna Pathi

Former Member
0 Kudos

Thnaks