ERPClimb logoERPClimb
CDS Views
Architect / Cross-trackintermediate 7 min read

CDS Views Part 3: Aggregation and Analytical Annotations

Preview · a free account unlocks the full lesson

Key takeaway

Aggregate data in CDS with SUM, COUNT, MIN, MAX, AVG and GROUP BY, understand why amounts must be grouped by currency, and see how analytical annotations turn CDS views into cubes and queries for embedded analytics.

In this lessonShow sections

Visual overview

CDS Views Part 3: Aggregation and Analytical Annotations — learning sheetFlow: Build ZI_SalesByCustomer: VBAK grouped by KUNNR and WAERK with SUM(NETWR) and COUNT(*) → Mark SoldToParty and TransactionCurrency as key → Read it from a classic report — one row per customer and currency → Build a small cube view with @Analytics.dataCategory: #CUBE and @Aggregation.default: #SUM on NetAmount. Key ideas: AGGREGATION FUNCTIONS; KEYS IN AGGREGATED VIEWS; CURRENCY AND UNIT RULE; STATIC AGGREGATION VS ANALYTICAL ENGINE. Watch out: Summing amounts across currencies produces a meaningless number that looks correct — the most common aggregation bug.ERPClimb.com / ARCH / LESSON 3/10 / LEARNING SHEETCDS Views Part 3: Aggregation and Analytical Annotations1 · HOW IT FLOWS1BuildZI_SalesByCustomerVBAK grouped by KUNNRand WAERK with…2Mark SoldToPartyandTransactionCurrency…3Read it from aclassic report —one row per…4Build a smallcube view with@Analytics.dataCategory…2 · KEY IDEAS EXPLAINEDAggregation Functionssum( field ), count( * ), count( distinct field ),min, max, avg( field as abap.dec(15,2) ).Every field that is not aggregated must be inGROUP BY.Keys In Aggregated ViewsThe key of an aggregated view is the GROUP BYcombination (e.g. customer + currency).Mark those fields as key so consumers and ODataknow what identifies a row.Currency And Unit RuleGroup amount totals by currency (WAERK) andquantities by unit (VRKME/MEINS).If one total in one currency is needed, convertfirst with currency_conversion, then aggregate —and know the performance cost (Part 10).Static Aggregation vs Analytical EngineA view with GROUP BY returns fixed totals — goodfor reports and ABAP consumers.Analytical models keep detail rows and let theanalytical engine aggregate at runtime, dependingon which dimensions the user drills into.3 · REMEMBER · WATCH OUTRememberCDS supports SUM, COUNT(*), COUNT(DISTINCT), MIN, MAX and AVG withGROUP BY and HAVING — aggregation runs in the database and returns few…Every non-aggregated field in the field list must appear in GROUP BY;amounts must be grouped by their currency field.Analytical annotations (@Analytics.dataCategory: #CUBE / #DIMENSION,@Aggregation.default) describe views for the analytical engine used by…!Watch outSumming amounts across currenciesproduces a meaningless number thatlooks correct — the most commonaggregation bug.4 · IN A REAL PROJECTA regional sales dashboard summed NETWR per customer without grouping by currency. A customer ordering in bothEUR and JPY showed a "total" of over 40 million — mostly yen added to euros. Management questionedthe whole dashboard. The fix grouped by WAERK in the CDS view and added a second view converting to group…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 2 calculated values per row; now we condense many rows into totals. CDS supports the standard aggregation functions — sum(field), count(*), count(distinct field), min, max, and avg(field as abap.dec(15,2)) — used together with GROUP BY and HAVING, with the aggregation itself running in the database so only a small result set comes back. Every field in the field list that isn't aggregated must appear in GROUP BY, and HAVING is what you use to filter on the aggregated values themselves, for example customers with more than 10 orders.

The key of an aggregated view is effectively the GROUP BY combination — for instance customer plus currency — so those fields should be marked as key so that consumers and OData know what identifies a row.

This connects directly to the most common aggregation bug: summing amounts across different currencies produces a number that looks correct but is actually meaningless, so amount totals must be grouped by their currency field (WAERK) and quantities by their unit field (VRKME/MEINS).

If you need one total in one currency, convert first with currency_conversion and aggregate afterward — and be aware there's a performance cost to that, covered in Part 10.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.