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:


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:


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:


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:


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:


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:


Advantages of Projection in CDS Views:

  1. Performance Optimization: By selecting only the required fields, you reduce the data load, leading to faster query execution.
  2. Simplified Data Models: By projecting only the necessary data, you make your data models simpler and more focused on the business requirements.
  3. Avoids Over-fetching Data: Instead of fetching all columns (which may include unnecessary data), projection ensures that only the required data is retrieved.
  4. 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.