ERPClimb logoERPClimb
CDS Associations
RAP / CDS / ODataintermediate 8 min read

CDS Associations Part 3: To-One vs To-Many — Semantics and Performance

Preview · a free account unlocks the full lesson

Key takeaway

Choose and use cardinality deliberately: what to-one and to-many mean for results, how to get one row per source when you need it, how to aggregate over to-many data, and how to keep text and time-dependent relations to-one.

In this lessonShow sections

Visual overview

CDS Associations Part 3: To-One vs To-Many — Semantics and Performance — learning sheetFlow: Create ZI_SalesOrderItemAgg (code example): count and sum per order and currency → Associate it from ZI_SalesOrder as to-one (per order and currency, or pick the order currency) → Replace any item fields in the list view with the aggregated values → Compare row counts and runtime before and after. Key ideas: WHAT CARDINALITY DOES; TO-ONE: SAFE IN LISTS; TO-MANY: DATA FOR NAVIGATION OR AGGREGATION; GETTING ONE ROW WHEN YOU NEED A SINGLE VALUE. Watch out: Declared cardinality is a promise — the database does not check it.ERPClimb.com / RAP / LESSON 3/5 / LEARNING SHEETCDS Associations Part 3: To-One vs To-Many — Semantics andPerformance1 · HOW IT FLOWS1CreateZI_SalesOrderItemAgg(code example)…2Associate itfromZI_SalesOrder as…3Replace any itemfields in thelist view with…4Compare rowcounts andruntime before…2 · KEY IDEAS EXPLAINEDWhat Cardinality DoesCardinality tells the compiler, the optimizer andframeworks (OData, Fiori, analytics) what toexpect. It is a promise, not a constraint: if you…To-One: Safe In ListsCustomer of an order, currency of an amount, plantof an item: at most one target row. You can usethese paths anywhere in the element list without…To-Many: Data For Navigation Or…navigation ($expand, object pages),aggregation (count, sum, min, max),Getting One Row When You Need A Single…Options, from best to worst:3 · REMEMBER · WATCH OUTRememberTo-one paths keep one row per source row; to-many paths can multiplyrows.Need one value from many rows? Aggregate in a separate view, or use afiltered path with "1:".Texts and time-dependent data become to-one through language/dateconditions.!Watch outDeclared cardinality is a promise —the database does not check it.4 · IN A REAL PROJECTA price list view declared its association to price conditions as [0..1]. In reality, overlapping validityperiods existed for about 2% of materials. A Fiori app trusted the cardinality and showed one price, but ananalytics query summing order values over the same view returned inflated totals because of duplicate rows…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 2 showed that path expressions become joins. This part is about the most important property of an association: how many target rows it can actually have per source row.

Cardinality tells the compiler, the optimizer, and frameworks like OData, Fiori and analytics tools what to expect from the relationship. It's a promise, not an enforced constraint — if [0..1] is declared but the target actually has two rows, duplicates still appear, and any consumer that trusted the promise ends up producing wrong results.

To-one associations are safe to use in lists. The customer of an order, the currency of an amount, the plant of an item — these have at most one target row, and paths through them can be used anywhere in the element list without changing the number of rows returned.

To-many associations, by contrast, are meant for navigation or aggregation rather than for flattening into a list. Items of an order, texts in all languages, price conditions — these all have many target rows, and to-many associations are the right tool for navigation ($expand, object pages), for aggregation (count, sum, min, max), and for existence-style filtering done in a controlled way. Unfiltered to-many paths should never go into list views.

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.