Aggregation Expression in SAP CDS view

SAP ABAP CDS(Core Data Services) view is a modeling tool in SAP system, which is used to define data model and database view. CDS views allow developers to create advanced data models that can extract data from multiple database tables and use them in SAP applications. In CDS Views, "Aggregation Expressions" is a powerful feature that allows developers to perform aggregation operations such as sum, count, average, etc. on data retrieved from underlying database tables.

@AggregationAggregate expressions are defined in CDS views using annotations. This way, you can transform raw data in database tables into more meaningful, aggregated results for easier use in reporting and analysis. Using the aggregation functions of CDS views, complex calculations can be performed at the database level, thereby improving query performance and reducing the burden on SAP applications.

Let us detail the Aggregation Expressions function of SAP ABAP CDS view with a concrete example.

Suppose we have a SAP system, in which there are two database tables: SalesOrderHeaderand SalesOrderItem, which store the data of sales order header and order line item respectively. Now, we want to create a CDS view that joins the data from these two tables and aggregates the order line items in order to calculate the total amount and average discount rate for each sales order.

First, we define the database table structure:

-- Sales Order Header Table
CREATE TABLE SalesOrderHeader (
    SalesOrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE
);

-- Sales Order Item Table
CREATE TABLE SalesOrderItem (
    ItemID INT PRIMARY KEY,
    SalesOrderID INT,
    ProductID INT,
    Quantity INT,
    UnitPrice DECIMAL(10,2),
    Discount DECIMAL(5,2)
);

Next, we create the CDS view and use @Aggregationannotations to aggregate order line items:

-- CDS View Definition
@AbapCatalog.sqlViewName: 'ZCDS_SalesOrder'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Sales Order CDS View'
define view ZSalesOrder as select from SalesOrderHeader
    association [0..*] to SalesOrderItem as _Item
        on $projection.SalesOrderID = _Item.SalesOrderID
{
    key SalesOrderHeader.SalesOrderID as SalesOrderID,
    SalesOrderHeader.CustomerID as CustomerID,
    SalesOrderHeader.OrderDate as OrderDate,
    
    // Aggregation Expressions
    @Aggregation.sum: @_Item.Quantity
    @ObjectModel.text.element: 'TotalQuantity'
    @_Item.Quantity as TotalQuantity,
    
    @Aggregation.sum: @_Item.Quantity * @_Item.UnitPrice * (1 - @_Item.Discount / 100)
    @ObjectModel.text.element: 'TotalAmount'
    @_Item.Quantity * @_Item.UnitPrice * (1 - @_Item.Discount / 100) as TotalAmount,
    
    @Aggregation.avg: @_Item.Discount
    @ObjectModel.text.element: 'AverageDiscount'
    @_Item.Discount as AverageDiscount
}
group by SalesOrderHeader.SalesOrderID, SalesOrderHeader.CustomerID, SalesOrderHeader.OrderDate;

In the CDS view definition above, we used @Aggregationannotations to define three aggregation expressions:

  1. TotalQuantity: Use the annotation to sum the fields @Aggregation.sumof the order line items to get the total quantity for each sales order.Quantity
  2. TotalAmount: Calculates the total amount for each sales order by summing the fields @Aggregation.sumof the order line items using annotations . TotalAmountHere we apply the discount rate by (1 - Discount / 100)multiplying to get the discounted total amount.
  3. AverageDiscount: Use annotations to calculate the average value of the fields @Aggregation.avgof the order line items to get the average discount rate of each sales order.Discount

In the CDS view, we used associations _Itemto connect tables SalesOrderHeader. SalesOrderItemThen, we use aggregation expressions to calculate the and fields SalesOrderItemin the table to get the total quantity, total amount and average discount rate of each sales order.QuantityUnitPriceDiscount

Through the definition of the above CDS view, we can easily obtain the total quantity, total amount and average discount rate of each sales order in the SAP application without manually writing complex queries. Also, these calculations are performed at the database level, so query performance is also optimized.

To sum up, the Aggregation Expressions function of the SAP ABAP CDS view allows developers to define aggregation expressions in the CDS view to perform aggregation operations on the data in the underlying database table, such as sum, count, and average. This provides more meaningful data and simplifies data processing and report generation in SAP applications. By using @Aggregationannotations, these aggregation operations can be easily implemented in CDS views, improving the performance and efficiency of the application.

Guess you like

Origin blog.csdn.net/i042416/article/details/132025521