Ansicht
Dokumentation

ABENCDS_AGGREGATE_FUNCTIONS_V2 - CDS AGGREGATE FUNCTIONS V2

ABENCDS_AGGREGATE_FUNCTIONS_V2 - CDS AGGREGATE FUNCTIONS V2

rdisp/max_wprun_time - Maximum work process run time   CL_GUI_FRONTEND_SERVICES - Frontend Services  
This documentation is copyright by SAP AG.
SAP E-Book

- CDS View Entity, aggr_expr

... ${ MAX( $[ALL$|DISTINCT$] operand )
    $| MIN( $[ALL$|DISTINCT$] operand )
    $| AVG( $[ALL$|DISTINCT$] operand AS dtype )
    $| SUM( $[ALL$|DISTINCT$] operand )
    $| COUNT( DISTINCT operand )
    $| COUNT(*) $} ...


Effect

Aggregate expression in a SELECT statement of a CDS view entity. An aggregate expression calculates a single value from multiple rows of the operand operand. The operands can have any data type except LCHR, LRAW, STRING, RAWSTRING, or GEOM_EWKB. The following table shows the possible aggregate functions:

Aggregate Function Meaning Result Type
MAX Returns the greatest value of operand Data type of operand
MIN Returns the least value of operand Data type of operand
AVG Average value of operand. Must be specified with the addition AS dtype. The following data types are supported: INT1, INT2, INT4, INT8, FLTP, DEC, CURR, QUAN, D16N, and D34N. Must be specified explicitly after AS. Possible data types are DEC, CURR, QUAN, D16N,D34N, and FLTP.
SUM Sum of operand.The following data types are supported: INT1, INT2, INT4, INT8, FLTP, DEC, CURR, QUAN, D16N, and D34N. Data type of operand
COUNT If DISTINCT operand is specified, the number of distinct values of operand is counted; if * is specified, the number of rows in the result set is counted. INT4

If ALL is used, all rows in the result set are respected, which is the standard setting. If DISTINCT is used, only distinct values of operand are respected.

The following can be specified for operand:

  • A field of a data source data_source of the current CDS view entity
  • A case distinction CASE that returns a value
  • A cast expression
  • Any built-in function that returns a suitable data type

The data type of the result is defined when the CDS view entity is activated. It depends on the aggregate function used and the value range of the data types of the operands. The result of aggregate function AVG must always be specified with the addition AS dtype.

Amounts and Quantities in Aggregate Expressions

If the operand operand of an aggregate function is a CDS amount field, a CDS quantity field, or a CDS calculated quantity, the result type might require a reference annotation as well. The following table shows the result type depending on the operand type of all available aggregate functions.

Aggregate Function Type of Operand Result Type
MAX, MIN, SUM, AVG amount amount
MAX, MIN, SUM, AVG quantity quantity
MAX, MIN, SUM, AVG calculated quantity calculated quantity
COUNT amount, quantity, calculated quantity number of type INT4

Operand positions:

  • An aggregate expression can be used in the condition cds_cond of a complex case distinction as the operand lhs or as the operand rhs if a comparison operator is used as relational operator.

Notes

  • The names of the aggregate functions AVG, COUNT, MAX, MIN, and SUM are protected and cannot be used as self-defined names.

Example

The following CDS view entity demonstrates the usage of the aggregate expressions AVG, SUM, MIN, MAX, and COUNT(*). An alternative element name is defined for each aggregation. The program DEMO_CDS_AGGREGATE_VE fills the underlying database table, reads the view entity and returns the result.

Example

The following CDS view entity demonstrates the nesting of aggregate functions and built-in CDS functions. Built-in CDS functions can serve as operands of aggregate functions, as long as they return a result with a suitable data type.






rdisp/max_wprun_time - Maximum work process run time   Fill RESBD Structure from EBP Component Structure  
This documentation is copyright by SAP AG.

Length: 14122 Date: 20240423 Time: 191013     sap01-206 ( 116 ms )