In the world of SAP HANA and Core Data Services (CDS), data modeling plays a critical role in enabling efficient, semantic-rich access to enterprise data. Two fundamental concepts that help model relationships between entities in CDS views are Associations and Joins. Understanding when and how to use these mechanisms is essential for SAP developers and consultants working with SAP HANA Live, S/4HANA, and analytical reporting.
This article explores the concepts of associations and joins in CDS views, highlighting their differences, use cases, and best practices.
Joins in CDS views are explicit instructions to combine data from two or more tables or views based on specified conditions. They physically merge rows that meet the join criteria into a single result set.
define view Z_CDS_SalesOrder
as select from sales_order as so
inner join customer as c
on so.customer_id = c.customer_id
{
so.sales_order_id,
so.order_date,
c.customer_name
}
Here, the join physically combines sales_order with customer to produce a unified result.
Associations are semantic relationships defined between CDS entities, representing navigational links rather than immediate joins. They do not join data at the time of the CDS view execution unless explicitly accessed.
define view Z_CDS_SalesOrder
as select from sales_order as so
association [0..1] to customer as _Customer
on so.customer_id = _Customer.customer_id
{
so.sales_order_id,
so.order_date,
_Customer.customer_name
}
In this example, _Customer is an association pointing to the customer entity. The customer data is accessed only when explicitly selected.
| Aspect | Joins | Associations |
|---|---|---|
| Execution | Data combined immediately | Data fetched on-demand (lazy) |
| Use Case | When combined data is always needed | When related data is optionally accessed |
| Performance | May be costly if multiple joins | More efficient for large models |
| Syntax Complexity | Can make views complex | Keeps views modular and clean |
| Navigation | No native navigation support | Supports navigation and path expressions |
| Cardinality Control | Limited to join types | Explicit cardinality definitions |
Use Joins when:
Use Associations when:
Associations can be traversed in CDS queries using the dot notation to select fields from associated views.
select from Z_CDS_SalesOrder
{
sales_order_id,
order_date,
_Customer.customer_name
}
The association _Customer is resolved automatically by the runtime to join with the customer table when the field customer_name is selected.
Associations and joins are fundamental to building efficient, maintainable CDS views in SAP HANA Live and S/4HANA environments. While joins physically combine tables during query execution, associations offer semantic, on-demand navigation that helps build flexible and scalable data models. Mastering both concepts allows SAP professionals to optimize reporting, analytics, and application development workflows.