ERPClimb logoERPClimb

Code examples · CDS views · Beginner

CDS view entities: association, path expression and aggregation (revenue per customer)

The problem

Sales management wants 'total net revenue per customer and currency' without anyone writing joins in every report.

How we solve it

  1. 1Tablesorders + customers
  2. 2Order viewwith association
  3. 3Revenue viewSUM + GROUP BY
  4. 4Read in ABAPpath expression

The result

Two reusable CDS views (orders with an association, revenue per customer) and a console class that fills sample data and prints the result.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

Sales management wants 'total net revenue per customer and currency' without anyone writing joins in every report. The customer name must be reachable from the order view, but the join should only be executed when a consumer actually asks for the name.

2. How we design it

Start with one reusable base view per business concept (orders, customers), connect them with an association, and build reports on top of those views. The join is written once, in the association, and the database only performs it when a consumer asks for a field behind it.

Why we decided it this way

  1. 1Association instead of a fixed joinSeveral consumers reuse the relationship, and queries that never ask for the customer name never pay for the join.
  2. 2Currency is part of the key of the revenue viewAdding EUR and USD together would produce a meaningless number, so each currency gets its own row.
  3. 3Aggregation in its own viewReports can reuse it, and the order view stays a clean base for other uses.
  4. 4A console class to prove itYou see the numbers without building any UI.

Before you start

  • An ABAP system or ABAP Environment where you can create CDS view entities and database tables.
  • Two simple tables ZSALESORDER_D and ZCUSTOMER_D (definitions are in step 1).

3. The code, step by step

Step 1 of 4: Define the two tables

Create the small data model the views will read.

CDS · Dictionary tables · ZCUSTOMER_D, ZSALESORDER_D
// Customer table
@EndUserText.label : 'Demo customer'
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zcustomer_d {
  key client      : abap.clnt not null;
  key customer_id : abap.char(10) not null;
  name            : abap.char(40);
}
// Sales order table with an amount and currency
@EndUserText.label : 'Demo sales order'
@AbapCatalog.tableCategory : #TRANSPARENT
@AbapCatalog.deliveryClass : #A
@AbapCatalog.dataMaintenance : #RESTRICTED
define table zsalesorder_d {
  key client      : abap.clnt not null;
  key order_id    : abap.char(10) not null;
  customer_id     : abap.char(10);
  @Semantics.amount.currencyCode : 'zsalesorder_d.currency'
  net_amount      : abap.curr(15,2);
  currency        : abap.cuky;
}
Part by part (plain text)
  1. Customer table. One row per customer. The customer id is the key. A client field makes it client-dependent like normal SAP business data.
  2. Sales order table with an amount and currency. Amount fields in the dictionary must point to a currency field with @Semantics.amount.currencyCode. This is what lets later views and UIs show 100.00 EUR correctly instead of a bare number.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Two reusable CDS views (orders with an association, revenue per customer) and a console class that fills sample data and prints the result.

What you should see

Revenue view: C1 EUR 400.00 and C2 USD 80.00. Orders with customer name through the path expression: 1001 to 1003 show Acme GmbH, 1004 shows Globex Ltd.

Try it and check the result

  • Run the class with F9. First block: C1 EUR 400.00 and C2 USD 80.00. Second block: four orders, each with its customer name (Acme GmbH for 1001 to 1003, Globex Ltd for 1004).
  • Open ZI_CustomerRevenueDemo in ADT and press F8 (Data Preview) to see the aggregation without ABAP.
  • Remove the \_Customer-Name field from the second query and look at the executed SQL (ADT SQL console or trace): the join to the customer table disappears. That is the benefit of associations over fixed joins.

Common mistakes

  • Declaring [0..1] when the data really has several targets silently produces duplicate rows. Cardinality is a promise, not a database constraint.
  • Summing amounts without grouping by currency mixes EUR and USD.
  • Selecting from the table inside the aggregation view instead of the order view duplicates logic and breaks reuse.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP CDS view entities and ABAP SQL (ABAP Cloud and S/4HANA on-premise 2021+; check on your release).

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.