简体   繁体   中英

Why isn't it possible to do a subtotal over an integer in CL_SALV_TABLE?

Let's assume we have the following table in ABAP:

PERNR WORKHOURS Other_Fields
00012 12,00 -
00110 5,00 -
00120 22,00 -

PERNR is on its DB-table saved as a NUMC with leading 0's. Due to some must-have-features, the leading 0's must be deleted before displaying a SALV-Table with CL_SALV_TABLE (as it is not accessible later on since). So essentially we have the following example data:

PERNR WORKHOURS Other_Fields
12 12,00 -
110 5,00 -
120 22,00 -

Now I want to do the following:

  1. Do a total over WORKHOURS
  2. Do a subtotal over PERNR

But since PERNR is a NUMC, the result is the following:

PERNR WORKHOURS Other_Fields
110 5,00 -
12 12,00 -
120 22,00 -

AFAIK, NUMC is getting compared from left to right and then this order makes sense. Sadly, I cannot change this, when I create my own dataelement for this or my own domain (At least I didn't saw an option to...).

When I cast PERNR as an INTEGER or DEC however, the sorting works and the order is as expected. Additionaly there is no need to delete leading 0's since these datatypes don't display or have leading 0's. BUT when I try to do my Steps 1. and 2., I get the following Error when trying Step 2. : "Subtotals cannot be calculated on aggregatable columns"

...Why? I don't see a reason to not aggregate these values. I haven't found any documentation so far as to why this is not allowed (or possible) either. I found numerous websites that explain, how to do it by coding it, but when I try to do a subtotal via coding I get the same error, so I'm pretty stuck.

Is it somehow possible to still do a subtotal over the column PERNR?

EDIT: @Suncatcher Sry, I used a tabletype in my datadictionary and didn't wanted to make the Post too large. I'll try to define my coding here as short as possible. Structuretype and Tabletype that has this as stucture defined:

@EndUserText.label : 'Structure for test salv'
@AbapCatalog.enhancementCategory : #NOT_EXTENSIBLE
define type ZS_SALV_STRUCTURE {
pernr        : abap.numc(8);
workhours    : abap.dec(4,2);
other_fields : abap.string(256);
}

Tabletype is called ZT_SALV_STRUCTURE in this example. The Report is shortened as follows (Example where I delete leading 0's from NUMC):

REPORT zsalv_test.
DATA: gv_okcode TYPE sy-ucomm,
      gx_salv   TYPE REF TO cl_salv_table,
      gt_data   TYPE zt_salv_structure.

START-OF-SELECTION.
  "Example 1: Delete leading 0's
  "Create example data to substitute the CDS-View
  gt_data = VALUE #( ( pernr = '012' workhours = 12 other_fields = '-' )
                     ( pernr = '110' workhours = 5  other_fields = '-' )
                     ( pernr = '120' workhours = 22 other_fields = '-' ) ).
  "Delete leading 0's
  LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<ls_data>).
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
      EXPORTING
        input  = <ls_data>-pernr
      IMPORTING
        output = <ls_data>-pernr.
  ENDLOOP.
  "Example 1 END

  "The Dynpro has gv_okcode for the OKCODE and is filled with a single custom container named 'CONTAINER'
  CALL SCREEN 0100.

*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  IF gx_salv IS INITIAL.
    cl_salv_table=>factory(
      EXPORTING
        r_container    = NEW cl_gui_custom_container( container_name = 'CONTAINER' )
        container_name = 'CONTAINER'
      IMPORTING
        r_salv_table   = gx_salv
      CHANGING
        t_table        = gt_data
    ).
    gx_salv->get_functions( )->set_all( abap_true ).
    gx_salv->get_columns( )->set_optimize( abap_true ).
    DATA(lt_columns) = gx_salv->get_columns( )->get( ).
    LOOP AT lt_columns ASSIGNING FIELD-SYMBOL(<ls_column>).
      CASE <ls_column>-columnname.
        WHEN 'PERNR'.
          <ls_column>-r_column->set_long_text( 'PERNR' ).
        WHEN 'WORKHOURS'.
          <ls_column>-r_column->set_long_text( 'WORKHOURS' ).
        WHEN 'OTHER_FIELDS'.
          <ls_column>-r_column->set_long_text( 'OTHER_FIELDS' ).
      ENDCASE.
    ENDLOOP.
    gx_salv->display( ).
  ELSE.
    gx_salv->refresh( ).
  ENDIF.

ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

ENDMODULE.

When I use INTEGER or DEC for PERNR, I change the Structuretype as follows and don't delete the leading 0's: INTEGER:

pernr        : abap.int4;

DEC:

pernr        : abap.dec(8,0);

And I "cast" my data in my Consumption-View via the CAST -Expression INTEGER:

cast(PersonnelNumber as abap.int4) as pernr;

DEC:

cast(PersonnelNumber as abap.dec(8,0) as pernr;

As for how I am sorting and do a (sub-)total: I use the function provided by the CL_SALV_TABLE class on the dynpro while running the report (Small buttons on top of the SALV).

Subtotal it's only on numeric data type. NUMC it's a char like field that allow only numbers but it's a char.

If you have this table... | PERNR | Month | Hours | |-------|-------|-------| | 0010 | Dec. | 160 | | 0010 | Nov. | 100 | | 0020 | Dec. | 80 |

You must have this Subtotals options Group by PERNR | PERNR | Month | Hours | |-------|-------|-------| | 0010 | | 260 | | 0020 | | 80 |

Group by Month | PERNR | Month | Hours | |-------|-------|-------| | | Dec. | 240 | | | Nov. | 100 |

If you wanna show PERNR with zero leadings, you can force ALPHA as conversion exit in the fieldcat.

Best regards.

Sebastian

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM