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: 

SALV - sorting and aggregation

Flavio
Active Contributor

Dear all,

I've the following question concerning the Salv model with sorting and aggregation: assuming to have the sorting, as follows:

1. 'Type of report' (i.e. Production / Shipment)
2. 'Plant'
3. 'Family of product' (i.e. Raw material / SemiFinished / Finished / etc)

and doing aggregation (sum) on these, I'm wondering if it's possible to add an extra level for again the 'Family of product', just before the 'Type of report';

that is to say, before having the grand total about, for instance, 'Production', I would like to have the total for all Plants concerning the 'Families';

I hope I've been clear enough...

Any help is highly appreciated!

Thanks and best regards,

Flavio

5 REPLIES 5

abo
Active Contributor

An intermediate summation level? Not sure it can be done but I'm following the question out of curiosity 🙂

Sandra_Rossi
Active Contributor
0 Kudos

Could you tell us again what result you expect? Below is standard output:

      Type   Plant  Family  Number1
      Prod   PL1    FAM1    10
      Prod   PL1    FAM1    15
Total Prod   PL1    FAM1    25
      Prod   PL1    FAM2     7
Total Prod   PL1    FAM2     7
Total Prod   PL1            32
      Prod   PL2    FAM1     1
Total Prod   PL2    FAM1     1
Total Prod   PL2             1
Total Prod                  33
Total                       33

Flavio
Active Contributor

thank you for the interest, sandra.rossi

here the expected result, if possible (added FAM1 & FAM2 subtotals, before 'Total Prod'):

      Type   Plant  Family  Number1
      Prod   PL1    FAM1    10
      Prod   PL1    FAM1    15
Total Prod   PL1    FAM1    25
      Prod   PL1    FAM2     7
Total Prod   PL1    FAM2     7
Total Prod   PL1            32
      Prod   PL2    FAM1     1
Total Prod   PL2    FAM1     1
Total Prod   PL2             1
Total Prod          FAM1    26
Total Prod          FAM2     7
Total Prod                  33
Total                       33

touzik_itc
Active Participant

One option is to calculate subtotals without aggregations and sorts:

TYPES: BEGIN OF t_rec,
         col1 TYPE string,
         col2 TYPE string,
         col3 TYPE string,
         num  TYPE i,
         txt  TYPE string,
       END OF t_rec,
       tt_rec    TYPE TABLE OF t_rec WITH KEY primary_key COMPONENTS col1 col2 col3 txt,
       tt_refrec TYPE TABLE OF REF TO tt_rec WITH EMPTY KEY.

DATA(lt_outtab) = VALUE tt_rec( ( col1 = 'Prod' col2 = 'PL1' col3 = 'FAM1' num = 10 )
                                ( col1 = 'Prod' col2 = 'PL1' col3 = 'FAM1' num = 15 )
                                ( col1 = 'Prod' col2 = 'PL1' col3 = 'FAM2' num = 7  )
                                ( col1 = 'Prod' col2 = 'PL2' col3 = 'FAM1' num = 1  ) ).

DATA(lv_columns) = 3.
DATA(lv_subtotals) = ipow( base = 2 exp = lv_columns ).
DATA(lt_subtotals) = VALUE tt_refrec( ).

DO lv_subtotals TIMES.
  APPEND NEW #( ) TO lt_subtotals.
ENDDO.

LOOP AT lt_outtab ASSIGNING FIELD-SYMBOL(<ls_outtab>).
  DO lv_subtotals TIMES.
    DATA(lv_subtotal_index) = sy-index.
    DATA(ls_subtotal) = VALUE #( BASE <ls_outtab>
      txt = COND #( WHEN lv_subtotal_index = lv_subtotals THEN 'total' ELSE 'subtotal' ) ).

    DO lv_columns TIMES.
      DATA(lv_column_index) = sy-index.
      ASSIGN COMPONENT lv_column_index OF STRUCTURE ls_subtotal TO FIELD-SYMBOL(<ls_column>).

      DATA(lv_mask1) = CONV hex02( lv_subtotal_index - 1 ).
      DATA(lv_mask2) = CONV hex02( ipow( base = 2 exp = ( lv_columns - lv_column_index ) ) ).
      DATA(lv_test) =  CONV hex02( lv_mask1 BIT-AND lv_mask2 ).

      <ls_column> = COND string( WHEN lv_test IS INITIAL THEN <ls_column> ELSE space  ).
    ENDDO.

    ASSIGN lt_subtotals[ lv_subtotal_index ]->* TO FIELD-SYMBOL(<lt_subtotals>).
    COLLECT ls_subtotal INTO <lt_subtotals>.
  ENDDO.
ENDLOOP.

LOOP AT lt_subtotals ASSIGNING FIELD-SYMBOL(<lr_subtotals>).
  APPEND LINES OF <lr_subtotals>->* TO lt_outtab.
ENDLOOP.

cl_salv_table=>factory( IMPORTING r_salv_table = DATA(gr_table) CHANGING t_table = lt_outtab ).
gr_table->display( ).

Here is the result:

Prod	PL1	FAM1	10	
Prod	PL1	FAM1	15	
Prod	PL1	FAM2	7	
Prod	PL2	FAM1	1	
Prod	PL1	FAM1	25	subtotal
Prod	PL1	FAM2	7	subtotal
Prod	PL2	FAM1	1	subtotal
Prod	PL1		32	subtotal
Prod	PL2		1	subtotal
Prod		FAM1	26	subtotal
Prod		FAM2	7	subtotal
Prod			33	subtotal
	PL1	FAM1	25	subtotal
	PL1	FAM2	7	subtotal
	PL2	FAM1	1	subtotal
	PL1		32	subtotal
	PL2		1	subtotal
		FAM1	26	subtotal
		FAM2	7	subtotal
			33	total

Flavio
Active Contributor
0 Kudos

Thank you touzik_itc, will try it