Application Development and Automation 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: 
Read only

Sorting in ALV with multiple fields

Former Member
0 Likes
5,831

Hi All,

I have a requirement to sort an ALV grid and get the subtotal according to an unique combination of keys.

The subtotal should be based on an unique combination of

cost center, internal order and product.

For example:

cost center, internal order, product, amount

25110 550145 100 50000

25110 550145 100 75000

Cost center 25110 125000

25110 550145 200 5000

Cost center 25110 5000

25110 650021 300 30000

Cost center 25110 30000

35100 550145 150 50000

35100 550145 200 6000

Cost center 35100 56000

Edited by: The Iceman on Aug 18, 2008 11:38 AM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,323

This is what i am doing currently; it wont sort properly, am i doing something wrong here?

wa_sort-fieldname = 'RZZFPROD1'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '01'.

wa_sort-up = 'X'.

  • wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

wa_sort-fieldname = 'USER0'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '02'.

wa_sort-up = 'X'.

  • wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

wa_sort-fieldname = 'RCNTR1'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '03'.

wa_sort-up = 'X'.

wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

19 REPLIES 19
Read only

Former Member
0 Likes
3,323
Read only

Former Member
0 Likes
3,323

>The subtotal should be based on an unique combination of

>cost center, internal order and product.

in that case you populate the sort table based on the fields cost center, internal order and product.

sort-fieldname = 'KOSTL'.
sort-up = 'X'.
append sort to it_sort.
sort-fieldname = 'AUFNR'.
sort-up = 'X'.
append sort to it_sort.
sort-fieldname = 'MATNR'.
sort-up = 'X'.
sort-subtot = 'X'.
append sort to it_sort.

this will take care.

pass this sort table to ALV .

Read only

Former Member
0 Likes
3,323

Hi,

Just go through the below link

http://www.sapdev.co.uk/reporting/alv/alvgrid_sort.htm

Thanks,

Ruthra

Read only

Former Member
0 Likes
3,323

Brian "The Iceman"??

pk

Read only

Former Member
0 Likes
3,323

Hi,

try to fill sort catelogue in the following manner

DATA:

fs_sort_data TYPE slis_sortinfo_alv.

DATA:

t_sort_data TYPE slis_t_sortinfo_alv.

fs_sort_data-fieldname = 'COSTCENTRE'.

fs_sort_data-spos = '01'.

fs_sort_data-up = c_flag_x.

APPEND fs_sort_data TO t_sort_data.

fs_sort_data-fieldname = 'IORDER'.

fs_sort_data-spos = '02'.

fs_sort_data-up = c_flag_x.

APPEND fs_sort_data TO t_sort_data.

fs_sort_data-fieldname = 'PRODUCT'.

fs_sort_data-spos = '03'.

fs_sort_data-up = c_flag_x.

APPEND fs_sort_data TO t_sort_data.

And then supply table t_sort_data to it_sort in REUSE_ALV_GRID_DISPALY

Regards.

Eshwar.

Read only

Former Member
0 Likes
3,323

Hi,

Use sort table.

data: ls_sort type slis_sortinfo_alv.

use as the following for which you want the sum for the key fields and append to sort internal table.Here in your case, use for first three fields you mentioned.

ls_sort-fieldname = 'fieldname'.

ls_sort-up = 'X'.

ls_sort-subtot = 'X'.

Regards,

S Raju

Read only

Former Member
0 Likes
3,323

Thank you all, i have done the same way that you people have pointed out but it still doesnt work

it gives the subtotal only for a single field like cost center and not for a unique combination for all three

this is wat it gives

cost center, internal order, product, amount

25110 550145 100 50000

25110 550145 100 75000

25110 550145 200 5000

25110 650021 300 30000

Cost center 25110 160000

35100 550145 150 50000

35100 550145 200 6000

Cost center 35100 56000

This is what i want

cost center, internal order, product, amount

25110 550145 100 50000

25110 550145 100 75000

Cost center 25110 125000

25110 550145 200 5000

Cost center 25110 5000

25110 650021 300 30000

Cost center 25110 30000

35100 550145 150 50000

35100 550145 200 6000

Cost center 35100 56000

Read only

0 Likes
3,323

have you tried the Collect statement??

pk

Read only

0 Likes
3,323

Try to check the Sub-total field for the LAST field in sort order only.

Regards

PS: just as Vijay Babu Dudla wrote.

Edited by: Raymond Giuseppi on Aug 18, 2008 12:08 PM

Read only

Former Member
0 Likes
3,323

the collect statement would work but then i would have to change the processing in the internal table.

isnt there a way to do it through ALV directly?

Read only

0 Likes
3,323

not sure if this is what you want...but try it.

once your ALV is displayed, select the columns cost center, internal order, product using the CNTRL key and press the button "Ascending Sort". The once again select all the three columns and then click the button Subtotals(sigma/sigma).

pk

Read only

0 Likes
3,323

lol! that is totally possible but i would like to do it programatically

Read only

0 Likes
3,323

sorry for stating the obvious... but you never know with people with the kind of posts you get here

pk

Read only

Former Member
0 Likes
3,324

This is what i am doing currently; it wont sort properly, am i doing something wrong here?

wa_sort-fieldname = 'RZZFPROD1'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '01'.

wa_sort-up = 'X'.

  • wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

wa_sort-fieldname = 'USER0'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '02'.

wa_sort-up = 'X'.

  • wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

wa_sort-fieldname = 'RCNTR1'.

wa_sort-tabname = 'INT_OUTPUT'.

  • wa_sort-spos = '03'.

wa_sort-up = 'X'.

wa_sort-subtot = 'X'.

APPEND wa_sort TO int_sort.

CLEAR wa_sort.

Read only

0 Likes
3,323

I tried from my side with a small code,. check it once..

REPORT  ztest_alv_sort.

TYPE-POOLS: slis.
DATA: BEGIN OF it_data OCCURS 0,
       kostl LIKE csks-kostl,
       aufnr LIKE aufk-aufnr,
       matnr LIKE vbap-matnr,
       qty   LIKE vbap-kwmeng,
     END OF it_data.

DATA:
it_fieldcat TYPE  slis_t_fieldcat_alv,
wa_fcat LIKE LINE OF it_fieldcat,
it_sort TYPE  slis_t_sortinfo_alv,
sort LIKE LINE OF it_sort.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
  EXPORTING
    i_program_name         = sy-repid
    i_internal_tabname     = 'IT_DATA'
    i_inclname             = sy-repid
  CHANGING
    ct_fieldcat            = it_fieldcat
  EXCEPTIONS
    inconsistent_interface = 1
    program_error          = 2.
wa_fcat-do_sum = 'X'.
MODIFY it_fieldcat FROM wa_fcat TRANSPORTING do_sum
WHERE fieldname = 'QTY'.



it_data-kostl = '25110'.
it_data-aufnr = '550145'.
it_data-matnr = '50000'.
it_data-qty = '100.00'.
APPEND it_data.

it_data-kostl = '25110'.
it_data-aufnr = '550145'.
it_data-matnr = '75000'.
it_data-qty = '100.00'.
APPEND it_data.

it_data-kostl = '25110'.
it_data-aufnr = '650021'.
it_data-matnr = '30000'.
it_data-qty = '300.00'.
APPEND it_data.


it_data-kostl = '35100'.
it_data-aufnr = '550145'.
it_data-matnr = '50000'.
it_data-qty = '100.00'.
APPEND it_data.
it_data-kostl = '35100'.
it_data-aufnr = '550145'.
it_data-matnr = '60000'.
it_data-qty = '100.00'.
APPEND it_data.

sort-fieldname = 'KOSTL'.
sort-up = 'X'.

APPEND sort TO it_sort.
CLEAR sort.
sort-fieldname = 'AUFNR'.
sort-up = 'X'.
APPEND sort TO it_sort.
sort-fieldname = 'MATNR'.
sort-up = 'X'.
sort-subtot = 'X'.
APPEND sort TO it_sort.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program = sy-repid
    it_fieldcat        = it_fieldcat
    it_sort            = it_sort
  TABLES
    t_outtab           = it_data
  EXCEPTIONS
    program_error      = 1.

Read only

0 Likes
3,323

I wouldnt do something revolutionary-ily different from what you,ve done. Sadly, dont have the luxury of debugging your code to check whts amiss.

pk

Read only

0 Likes
3,323

Hi! thanks a lot! it worked!

Read only

Former Member
0 Likes
3,323

Hi,

use SLIS_SORTINFO_ALV for creating one internal table.

then fill this table with waht ever the fields you want to sort.

then add this table to final alv function module(reuse_alv_list_display)

Regards,

venkat

Read only

Subhankar
Active Contributor
0 Likes
3,323

Hi

Do it in this way

TYPE-POOLS: slis.

TYPES: BEGIN OF x_marc,

matnr TYPE matnr,

werks TYPE werks_d,

abc TYPE char10,

losfx TYPE losfx,

END OF x_marc.

DATA: i_marc TYPE STANDARD TABLE OF x_marc INITIAL SIZE 0,

i_marc1 TYPE STANDARD TABLE OF x_marc INITIAL SIZE 0,

wa_marc TYPE x_marc,

i_fieldcat TYPE slis_t_fieldcat_alv, "Field Catalog

wa_layout TYPE slis_layout_alv, "Field Layout

wa_fieldcat TYPE slis_fieldcat_alv, "Field Catalog

l_losfx TYPE losfx,

l_tabix TYPE i.

wa_marc-matnr = '01'.

wa_marc-werks = '11'.

wa_marc-abc = 'AAA'.

wa_marc-losfx = '1000'.

APPEND wa_marc TO i_marc.

CLEAR: wa_marc.

wa_marc-matnr = '01'.

wa_marc-werks = '11'.

wa_marc-abc = 'AAA'.

wa_marc-losfx = '2000'.

APPEND wa_marc TO i_marc.

CLEAR: wa_marc.

wa_marc-matnr = '01'.

wa_marc-werks = '11'.

wa_marc-abc = 'BBB'.

wa_marc-losfx = '1000'.

APPEND wa_marc TO i_marc.

CLEAR: wa_marc.

wa_marc-matnr = '01'.

wa_marc-werks = '11'.

wa_marc-abc = 'CCC'.

wa_marc-losfx = '1000'.

APPEND wa_marc TO i_marc.

CLEAR: wa_marc.

SORT i_marc BY matnr werks abc.

LOOP AT i_marc INTO wa_marc.

l_losfx = l_losfx + wa_marc-losfx.

APPEND wa_marc TO i_marc1.

AT END OF abc.

CLEAR: wa_marc.

wa_marc-matnr = 'Total'.

wa_marc-losfx = l_losfx.

APPEND wa_marc TO i_marc1.

CLEAR l_losfx.

ENDAT.

ENDLOOP.

  • Prepare ALV field catalog

PERFORM sub_populate_fieldcat USING :

'MATNR' '18' 'Material Number'(008),

'WERKS' '4' 'plant'(009),

'ABC' '10' 'type'(010),

'LOSFX' '20' 'Amount'.

  • Layout

wa_layout-zebra = 'X'.

  • Optimize the column width

wa_layout-colwidth_optimize = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

is_layout = wa_layout

it_fieldcat = i_fieldcat[]

TABLES

t_outtab = i_marc1[]

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

&----


*& Form sub_populate_fieldcat

&----


  • text

----


  • -->P_FIELDNAME text

  • -->P_LENGTH text

  • -->P_TEXT text

----


FORM sub_populate_fieldcat USING p_fieldname TYPE slis_fieldname

p_length TYPE dd03p-leng

p_text TYPE dd03p-scrtext_l.

CLEAR wa_fieldcat.

wa_fieldcat-tabname = 'I_MARC'. " Internal Table name

wa_fieldcat-fieldname = p_fieldname. " Field name

wa_fieldcat-outputlen = p_length. " Field length

wa_fieldcat-just = 'L'. " Position

wa_fieldcat-seltext_l = p_text. " Field text

  • Append word area to Internal table

APPEND wa_fieldcat TO i_fieldcat .

ENDFORM. " sub_populate_fieldcat

Edited by: Subhankar Garani on Aug 18, 2008 1:06 PM

Edited by: Subhankar Garani on Aug 18, 2008 1:07 PM