CDS Views Part 3: Aggregation and Analytical Annotations
Preview · a free account unlocks the full lesson
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 sectionsHide sections
Visual overview
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.
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
Interview questions on this topic
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.