ABAP SQL offers various SQL techniques to manipulate and combine data sets. Are they also supported in ABAP CDS? And if they are, what are the differences?
Here is a list of the most important SQL techniques:
- ORDER BY
The ORDER BY clause to sort result sets is not supported in ABAP CDS.
- DISTINCT
The DISTINCT addition is used to remove duplicate entries from the result set. It is supported in ABAP CDS but with the DISTINCT after keyword SELECT and not at the beginning of the field list.
- Aggregations and GROUP BY
Aggregations are used to derive values from an entire data set, for example the number of records, a maximum or an average value. Aggregations are often combined with a GROUP BY clause that defines groups of records before the aggregation is performed for each group separately. Not all aggregations that are available in ABAP SQL are also supported in CDS views, but the most important aggregations, like COUNT, MIN, MAX, SUM and AVG are available. There are also minor differences in syntax. For example, it is mandatory to specify the result type when calculating an average with AVG. In ABAP SQL this is only an option.
- JOIN
Joins are used to combine records from different result sets that match a join condition. The same join types are supported by ABAP SQL and ABAP CDS. Again, there are some minor syntax differences. It is mandatory to specify the data source when accessing fields and ABAP CDS uses a different field selector.
- UNION
The UNION statement is used to unite the records from different result sets. When using UNION in the definition of CDS views, special rules apply for the key definition and the types, names, and annotations of view elements.
Let us have a look at an example for each of these techniques.