In SAP ABAP CDS Views, projection refers to selecting and presenting a subset of fields (columns) from one or more database tables, views, or entities. The concept of projection is a key feature of CDS (Core Data Services), which allows you to define what data should be retrieved when a CDS view is executed.
In simpler terms, projection in a CDS view means you are choosing which fields (columns) to include in the result set. This allows you to optimize the query by selecting only the necessary fields rather than fetching all fields from the database.
Basic Projection Example
Here’s a basic example of a projection in a CDS view:
define view ZFLIGHT_PROJECTION as select from sflight { key carrid, // Projecting the 'carrid' field key connid, // Projecting the 'connid' field fldate, // Projecting the 'fldate' field price // Projecting the 'price' field}
Explanation:
- This CDS view
ZFLIGHT_PROJECTION
projects only the fieldscarrid
,connid
,fldate
, andprice
from thesflight
table. - By projecting these specific fields, the query becomes more efficient since only the required data is fetched, reducing unnecessary data retrieval.
Projection of Calculated Fields
You can also perform calculations or transformations on fields during the projection. For example, if you want to calculate a new field, you can project it directly in the view:
define view ZFLIGHT_PROJECTION_CALC as select from sflight { key carrid, key connid, fldate, price, price * 1.1 as price_with_tax // Calculating a new field with a projection}
Explanation:
- Here, the
price_with_tax
field is calculated asprice * 1.1
directly in the projection. This allows you to create derived fields as part of your query without needing separate logic in ABAP code.
Projection with Aliases
You can also use aliases to rename the projected fields or calculations for easier reference or to follow naming conventions.
define view ZFLIGHT_ALIAS as select from sflight { key carrid as Carrier, key connid as Connection, fldate as FlightDate, price as FlightPrice}
Explanation:
- The fields are projected from the
sflight
table, but their names are aliased (renamed) in the CDS view result set. carrid
becomesCarrier
,connid
becomesConnection
, and so on.
Projection with Aggregation
You can also use aggregation functions (e.g., SUM
, COUNT
, AVG
) during projection. For example, to calculate the total flight price for each carrier:
define view ZFLIGHT_AGGREGATION as select from sflight { key carrid, sum(price) as total_price // Projecting the aggregated price field}group by carrid
Explanation:
- In this case, the
SUM(price)
is projected, which gives you the total flight price per carrier. - The
group by
ensures the aggregation happens at thecarrid
level.
Projection with Associations
In CDS views, associations allow you to represent relationships between different entities (tables/views). You can use associations to project related data from associated tables.
Example: Using Associations in Projection
define view ZFLIGHT_ASSOCIATION as select from sflight { key carrid, key connid, fldate, price, association to ZAIRLINE as _airline on $projection.carrid = _airline.carrid}
Explanation:
- The
association to ZAIRLINE
allows you to project data from an associatedZAIRLINE
view, using thecarrid
to join the two entities. - Associations help in projecting data from related tables, which can be especially useful for normalized data models.
Projection with Conditional Expressions
You can use conditional expressions like CASE
or COALESCE
in your projections to manipulate data based on conditions.
Example: Using CASE
for Conditional Projection
define view ZFLIGHT_CASE as select from sflight { key carrid, key connid, fldate, price, case when price > 500 then 'Expensive' else 'Affordable' end as price_category}
Explanation:
- The
price_category
is projected based on a condition that checks whether theprice
is greater than 500. If it is, it is labeled asExpensive
; otherwise, it is labeled asAffordable
. - This is a way of projecting new data based on business logic.
Advantages of Projection in CDS Views:
- Performance Optimization: By selecting only the required fields, you reduce the data load, leading to faster query execution.
- Simplified Data Models: By projecting only the necessary data, you make your data models simpler and more focused on the business requirements.
- Avoids Over-fetching Data: Instead of fetching all columns (which may include unnecessary data), projection ensures that only the required data is retrieved.
- Flexibility: Projection allows you to transform or manipulate data in the query itself, reducing the need for additional logic in your application code.
Conclusion
Projection in SAP ABAP CDS Views allows you to select and present only the necessary fields, improving both performance and manageability. By leveraging features such as field aliasing, calculated fields, and associations, you can make your views more efficient and tailored to the exact data requirements of your application.