
In a previous part of this learning journey, you learned that with SAP HANA's enormous data analysis capabilities, you are recommended to do calculations already on the database whenever this is possible. You also learned that ABAP SQL offers plenty of expressions and built-functions for this purpose.
Most of these expressions and built-in functions are also available in CDS view definitions. This allows you to move the complicated calculations into a CDS view definition and then retrieve the data from the CDS view.
Let us look at an example. In this ABAP code, the ABAP SQL statement uses an arithmetic expression to calculate the number of free seats on a flight by subtracting the number of occupied seats from the total number of seats.

In this next example, the arithmetic expression is moved into a CDS view definition. The calculation result is visible to consumers of the view as view element seats_free. The ABAP code reads view element seats_free like any other view element.
Note
For the consumer of a CDS view, it makes no difference, if a view element is simply a projection of a database table field or the result of a complicated calculation.

Moving the SQL logic from ABAP SQL into a CDS view has several advantages:
- Encapsulation
Because the SQL logic is hidden in the CDS view definition, the ABAP SQL statements become easier to read.
- Reuse
By reading from the same CDS view, you can easily reuse the SQL logic in other applications. In particular, you can use the SQL logic in the frameworks that use CDS views, for example, embedded analytics, CDS-based data extractors for SAP BW, ABAP RESTful application programming and many more.
- Semantics
You can add semantics to the calculation result by giving the element a meaningful name and by annotating it with element annotations.
- Authorization Rules
You can restrict the access to the calculated data by creating a CDS access control for the CDS view.