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: 

queries on ALV Sub totalling

Former Member
0 Kudos
145

Hi all,

I have a requirement to change few fields in existing ALV.

1) i have data like this in ALV.

GROUP G/L ACCOUNT LC USD

100 10 5 5

100 11 6 4

100 12 3 3

100 11 3 2

200 10 5 5

200 11 6 4

200 12 3 3

200 11 3 2

now for particular GROUP (ex: 100) i need the sub totals of LC(5633=17) and USD(5432=14)

it should be shown before the GROUP 200 begins.

now the output should come as:

GROUP G/L ACCOUNT LC USD

100 10 5 5

11 6 4

12 3 3

11 3 2

TOTAL 17 14

200 10 5 5

11 6 4

12 3 3

11 3 2

TOTAL 17 14

Is there any way to do this..please help

thanks,

prasanth

1 ACCEPTED SOLUTION

Clemenss
Active Contributor
0 Kudos
104

Hi prasanth,

see yout other thread. Use the SORT parameter and subtotal option. For the summing up of diferent currencies you have to set the reference field for the currency key field in the fieldcatalog

Regards,

Clemens

14 REPLIES 14

Former Member
0 Kudos
104

Hi

If you want to a total sum, modify the fieldcate of the alv. In structure of fieldcate there is a component

named 'do_sum', give the value 'X'.

If you want to a subtotal, you should use sorting parameter, give value 'X' to subtot which is a

component of structure slis_sortinfo_alv.

For your situation, you should do following:

w_sort-filedname = 'GROUP'.
           w_sort-up = 'X'
           w_sort-subtot = 'X'
           append w_sort to i_sort.
           clear w_sort.

When call alv, pass i_sort to parameter IT_SORT.

Edited by: alex yu on Nov 22, 2008 6:16 PM

Edited by: alex yu on Nov 22, 2008 6:20 PM

0 Kudos
104

HI,

I have written the following code...still I am not getting the output..plz suggest any corrections.

DATA: l_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.

DATA: w_fieldcat like l_fieldcat,

l_sort type slis_t_sortinfo_alv,

l_layout type slis_layout-alv,

ls_sort type slis_sortinfo_alv.

DATA: w_col_pos_nm TYPE i VALUE 0.

****Build field catalog************************

There are 24 fields in my grid but I am only showing the fields for which I need sub totalling

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'HSLVT'.

w_fieldcat-seltext_l = text-008.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT'.

w_fieldcat-seltext_l = text-010.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT_NEW'.

w_fieldcat-seltext_l = text-013.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT_POST'.

w_fieldcat-seltext_l = text-014.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

****************Build sort table**********************************

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'BILKT'.

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

ls_SORT-SUBTOT = 'X'. "based on this field i need sub totalling

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'HSLVT'. "amount in local currency

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT'. "amount in USD

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_NEW'. "gap in USD

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_POST'. " GC difference

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

*******ALV for Display***************

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

is_layout = l_layout

it_fieldcat = l_fieldcat[].

it_sort = l_sort[]

TABLES

t_outtab = it_postab.

EXCEPTIONS

program_error = 1

OTHERS = 2.

I have used do_sum = 'X' in field catalog for the fields to which I need the sub totals and In the sort table population I have used SORT-SUBTOT = 'X'. for the "GROUP" field only based on which the other fields have to be sub totalled.

I am trying to do this based o your earlier suggetion...please correct me If I am wrong!

Edited by: prasanth pulapaka on Nov 22, 2008 9:41 PM

0 Kudos
104

Hi

here you just want a subtotal right? so when define fieldcate don't give 'X' to do_sum, just use sort.

Because you want to do subtotal based on group, when you define sort, just give 'GROUP' to the sort

table. Try following code:

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'HSLVT'.

w_fieldcat-seltext_l = text-008.

w_fieldcat-outputlen = 23.

APPEND w_fieldcat TO l_fieldcat.

......

......

****************Build sort table**********************************

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'BILKT'.

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

ls_SORT-SUBTOT = 'X'. "based on this field i need sub totalling

APPEND ls_SORT TO l_SORT.

****************Build sort table ends****************************

0 Kudos
104

Hi,

A very small doubt when I am gving this code..

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT_NEW'.

w_fieldcat-seltext_l = text-013.

w_fieldcat-outputlen = 23.

w_fieldcat-datatype = 'CURR'.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

In the line w_fieldcat-do_sum = u2018Xu2019. the compiler is saying that 'X' is unknown...

0 Kudos
104

Maybe you should pay attention to your single quotes. I see single quotes in the following statement is

different.

w_fieldcat-do_sum = u2018Xu2019.

ls_SORT-FIELDNAME = 'BILKT'.

0 Kudos
104

HAi,

as you suggested i have tried using sort table sort_subtot = 'X' for GROUP field only

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'BILKT'.

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

ls_SORT-SUBTOT = 'X'. "based on this field i need sub totalling

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'HSLVT'. "amount in local currency

ls_SORT-SPOS = 2.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT'. "amount in USD

ls_SORT-SPOS = 3.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_NEW'. "gap in USD

ls_SORT-SPOS = 4.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_POST'. " GC difference

ls_SORT-SPOS = 5.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

but it is not sub totalling the fields LC,amount in USD,gap in USd,GC diffenece

how to get those sub totals now ..plz help

0 Kudos
104

Hi

What's the data type of these fields? Are they type the data element currency? If yes, when you

define the fieldcate you can try

wa_fieldcat-fieldname = 'DMBTR'.
wa_fieldcat-ref_fieldname = 'DMBTR'.
wa_fieldcat-ref_tabname = 'BSEG'.
wa_fieldcat-cfieldname = 'WAERS'
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

0 Kudos
104

Hi ,

I am not getting the sub totals yet,the program is giving a dump when i am executing the Fm reuse_alv_grid_display....

when I am trying to keep do_sum = 'X' in field catalog for LC,USD fields I am getting dump,what are the necessary parameters that should be passed to fieldcatalog wen i give do_sum ='X'.

Iam in great confusion ,it is necessary to give do_sum = 'X' for the fields for which I need sub totals??

I am populating sort table with fields for which i need sub totals,so it means I need to do it both in F.Catalog and sort table.

plz help.

****Build field catalog************************

There are 24 fields in my grid but I am only showing the fields for which I need sub totalling

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'HSLVT'.

w_fieldcat-seltext_l = text-008.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT'.

w_fieldcat-seltext_l = text-010.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT_NEW'.

w_fieldcat-seltext_l = text-013.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

w_col_pos_nm = w_col_pos_nm + 1.

CLEAR w_fieldcat.

w_fieldcat-col_pos = w_col_pos_nm.

w_fieldcat-fieldname = 'KSLVT_POST'.

w_fieldcat-seltext_l = text-014.

w_fieldcat-outputlen = 23.

w_fieldcat-do_sum = u2018Xu2019.

APPEND w_fieldcat TO l_fieldcat.

****************Build sort table**********************************

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'BILKT'.

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

ls_SORT-SUBTOT = 'X'. "based on this field i need sub totalling

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'HSLVT'. "amount in local currency

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT'. "amount in USD

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_NEW'. "gap in USD

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

CLEAR ls_SORT.

ls_SORT-FIELDNAME = 'KSLVT_POST'. " GC difference

ls_SORT-SPOS = 1.

ls_SORT-UP = 'X'.

APPEND ls_SORT TO l_SORT.

*******ALV for Display***************

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

is_layout = l_layout

it_fieldcat = l_fieldcat[].

it_sort = l_sort[]

TABLES

t_outtab = it_postab.

EXCEPTIONS

program_error = 1

OTHERS = 2.

IS any thing else need to be passed to FM,coz I a getting dump with in the function module code..

Edited by: prasanth pulapaka on Nov 23, 2008 7:12 PM

0 Kudos
104

Hi

I am sorry, I have made a mistake...

As what I have said, If you want to a total sum of some fields, you need give 'X' to do_sum when you

define the fieldcate. At this time you can decide which field will be sum total.

But if you want to do subtotal, as what I have known, you use sort table here. Depend on which field

you want to do subtotal, then you just put this field to sort table, and give 'X' to subtot when you define

sort table. Also you should give 'X' to do_sum of which field you need subtotal.

You can reference the following code, I have test it. Hope it give you some help.

TYPE-POOLs: slis.

TYPES:
  begin of ty_final,
    bilkt type bilkt,
    hslvt type hslvt,
    kslvt type kslvt,
    kslvt_new type kslvt,
    kslvt_post type kslvt,
    test type i,
  end of ty_final.

DATA:
  i_final type standard table of ty_final,
  w_final type ty_final.


DATA:
  i_fieldcat type slis_t_fieldcat_alv,
  w_fieldcat type slis_fieldcat_alv,
  i_sort type slis_t_sortinfo_alv,
  w_sort type slis_sortinfo_alv.

PERFORM give_value.
PERFORM build_fieldcat.
PERFORM build_sort.
PERFORM call_alv.

form give_value.
  w_final-bilkt = '100'.
  w_final-hslvt = '10'.
  w_final-kslvt = '5'.
  w_final-kslvt_new = '5'.
  w_final-kslvt_post = '5.1'.
  w_final-test = '1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '100'.
  w_final-hslvt = '11'.
  w_final-kslvt = '6'.
  w_final-kslvt_new = '4'.
  w_final-kslvt_post = '5.1'.
  w_final-test = '1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '100'.
  w_final-hslvt = '12'.
  w_final-kslvt = '3'.
  w_final-kslvt_new = '3'.
  w_final-kslvt_post = '5.1'.
  w_final-test = '1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '100'.
  w_final-hslvt = '11'.
  w_final-kslvt = '3'.
  w_final-kslvt_new = '2'.
  w_final-kslvt_post = '5.1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '200'.
  w_final-hslvt = '10'.
  w_final-kslvt = '5'.
  w_final-kslvt_new = '5'.
  w_final-kslvt_post = '1.1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '200'.
  w_final-hslvt = '11'.
  w_final-kslvt = '6'.
  w_final-kslvt_new = '4'.
  w_final-kslvt_post = '1.1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '200'.
  w_final-hslvt = '12'.
  w_final-kslvt = '3'.
  w_final-kslvt_new = '3'.
  w_final-kslvt_post = '1.1'.
  APPEND w_final to i_final.
  CLEAR w_final.

  w_final-bilkt = '200'.
  w_final-hslvt = '11'.
  w_final-kslvt = '3'.
  w_final-kslvt_new = '2'.
  w_final-kslvt_post = '1.1'.
  APPEND w_final to i_final.
  CLEAR w_final.
endform.

form build_fieldcat.
  w_fieldcat-fieldname = 'BILKT'.
  w_fieldcat-seltext_l = 'Group'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname = 'HSLVT'.
  w_fieldcat-seltext_l = 'Column1'.
  w_fieldcat-do_sum = 'X'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname = 'KSLVT'.
  w_fieldcat-seltext_l = 'Column2'.
  w_fieldcat-do_sum = 'X'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname = 'KSLVT_NEW'.
  w_fieldcat-seltext_l = 'Column3'.
  w_fieldcat-do_sum = 'X'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname = 'KSLVT_POST'.
  w_fieldcat-seltext_l = 'Column4'.
  w_fieldcat-do_sum = 'X'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

  w_fieldcat-fieldname = 'TEST'.
  w_fieldcat-seltext_l = 'Test'.
  w_fieldcat-do_sum = 'X'.
  APPEND w_fieldcat to i_fieldcat.
  CLEAR w_fieldcat.

endform.

form build_sort.
  w_sort-fieldname = 'BILKT'.
  w_sort-up = ''.
  w_sort-subtot = 'X'.
  APPEND w_sort to i_sort.
  clear w_sort.
endform.

form call_alv.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      IT_FIELDCAT                       = i_fieldcat
      IT_SORT                           = i_sort
    TABLES
      t_outtab                          = i_final
            .

endform.

Edited by: alex yu on Nov 24, 2008 11:14 AM

Clemenss
Active Contributor
0 Kudos
105

Hi prasanth,

see yout other thread. Use the SORT parameter and subtotal option. For the summing up of diferent currencies you have to set the reference field for the currency key field in the fieldcatalog

Regards,

Clemens

Former Member
0 Kudos
104

Hi Li,

I didn't get what your trying to say..I need to give reference of table field in field catalog for suming up the currency..Is that youwant to say.

If you dont mind plz explain how to do that with some code.please

GauthamV
Active Contributor
0 Kudos
104

hi,

if you want total based on currency field then use cfieldname field in fielcatelog.

define this varaible in fieldcatelog for which you want total.


ex:
wa_fieldcat-fieldname = 'DMBTR'.
wa_fieldcat-ref_fieldname = 'DMBTR'.
wa_fieldcat-ref_tabname = 'BSEG'.
wa_fieldcat-cfieldname = 'WAERS'
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.

former_member598013
Active Contributor
0 Kudos
104

Hello Prashant,

If you need to display the Subtotal then you set one property which is SUBTOT In the fieldcatalog for that field on which you need to display the subtotal.

Also you need to sort the table in that sequence on which you want the Subtotal.


FIELDCAT-SUBTOT = 'X'.

Thanks,

Chidanand

Former Member
0 Kudos
104

Hai Prasanth,

Try this.

LR_SORTS = LR_TABLE->GET_SORTS( ).

LR_AGGREGATIONS = LR_TABLE->GET_AGGREGATIONS( ).

PERFORM SET_AGGREGATIONS USING LR_AGGREGATIONS '<lc fieldname>'.

PERFORM SET_SORTS USING LR_SORTS '<group filedname>' 1

1 'X' .

Double click SET_AGGREGATIONS

&----


*& Form SET_AGGREGATIONS

&----


  • text

----


  • -->P_LR_AGGREGATIONS text

  • -->P_0834 text

----


FORM SET_AGGREGATIONS USING IR_AGGREGATIONS TYPE REF TO CL_SALV_AGGREGATIONS

L_COL TYPE ANY.

CONSTANTS: L_1 TYPE SALV_DE_AGGREGATION VALUE '1'.

TRY.

IR_AGGREGATIONS->SET_NUMERICAL_AGGREGATION( ABAP_FALSE ).

TRY.

IR_AGGREGATIONS->ADD_AGGREGATION(

COLUMNNAME = L_COL

AGGREGATION = L_1 ).

CATCH CX_SALV_DATA_ERROR CX_SALV_EXISTING CX_SALV_NOT_FOUND.

MESSAGE I023(SALV_EXCEPTION).

CATCH CX_SALV_METHOD_NOT_SUPPORTED.

MESSAGE I028(SALV_EXCEPTION) WITH SPACE SPACE SPACE SPACE.

ENDTRY.

CATCH CX_SALV_METHOD_NOT_SUPPORTED.

MESSAGE I028(SALV_EXCEPTION) WITH SPACE SPACE SPACE SPACE.

ENDTRY.

ENDFORM. " SET_AGGREGATIONS

Double click SET_SORTS

&----


*& Form SET_SORTS

&----


  • text

----


  • -->P_LR_SORTS text

  • -->P_0827 text

  • -->P_1 text

  • -->P_1 text

  • -->P_0830 text

----


FORM SET_SORTS USING IR_SORTS TYPE REF TO CL_SALV_SORTS

L_COL TYPE ANY

L_POS TYPE I

L_SEQ TYPE SALV_DE_SORT_SEQUENCE

L_TOT TYPE SAP_BOOL.

TRY.

IR_SORTS->SET_GROUP_ACTIVE( ABAP_FALSE ).

TRY.

IR_SORTS->ADD_SORT(

COLUMNNAME = L_COL

POSITION = L_POS

SEQUENCE = L_SEQ

SUBTOTAL = L_TOT

).

CATCH CX_SALV_DATA_ERROR CX_SALV_EXISTING CX_SALV_NOT_FOUND.

MESSAGE I023(SALV_EXCEPTION).

CATCH CX_SALV_METHOD_NOT_SUPPORTED.

MESSAGE I028(SALV_EXCEPTION) WITH SPACE SPACE SPACE SPACE.

ENDTRY.

CATCH CX_SALV_METHOD_NOT_SUPPORTED.

MESSAGE I028(SALV_EXCEPTION) WITH SPACE SPACE SPACE SPACE.

ENDTRY.

ENDFORM. " SET_SORTS