Sub topic 7
Building an analytical query view
This chapter assumes you can read ABAP CDS view entities and associations. It also assumes you know the cube and dimension pattern from the previous chapters, and that you understand measures with currency semantics. Here you will build the query view that sits on top of a cube and makes it usable in analytical front ends.
What a query view does
A cube holds the facts and a dimension holds master data. Neither decides what a report shows. The analytical query view does that. It belongs to the consumption layer of the Virtual Data Model. It reads one cube, picks the fields a user needs, and says which fields go on the rows, which go on the columns, and which are filters.
A query contains no data of its own. At run time the analytical engine turns the query into one SQL statement against the live tables. The engine applies the aggregation you declared on the cube measures, so you do not write a GROUP BY.
The minimal query
The query below reads the cube from the earlier chapters. It puts the customer on the rows and the net amount on the columns.
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Net amount by customer'
@Analytics.query: true
define view entity ZC_NetAmountByCustomer
as select from ZI_SalesItemCube
{
@AnalyticsDetails.query.axis: #ROWS
CustomerId,
@AnalyticsDetails.query.axis: #COLUMNS
NetAmount
}Four things make this a query:
@Analytics.query: truetells the framework to treat the view as an analytical query.as select from ZI_SalesItemCubereads the cube, not the table. The query inherits its measures, aggregation and associations.@AnalyticsDetails.query.axisassigns each element to#ROWS,#COLUMNSor#FREE.@AccessControl.authorizationCheck: #CHECKrequires an access control to exist for the view. The authorization chapter covers it.
The measure NetAmount carries @DefaultAggregation: #SUM and a link to Currency on the cube. You do not repeat either on the query. If the result mixes currencies, the engine does not add the amounts together. It shows them separately per currency.
Choosing axes
An element on #ROWS or #COLUMNS appears when the query opens. An element on #FREE is hidden at first, and the user can drag it into the result later. Use #FREE for secondary attributes, so the first view stays readable.
| Axis value | Where the element appears | Typical use |
|---|---|---|
| `#ROWS` | Rows of the result | Customer, product, month |
| `#COLUMNS` | Columns of the result | Measures such as net amount |
| `#FREE` | Available, not shown at first | Country, sales organization |
You can add @AnalyticsDetails.query.display and @AnalyticsDetails.query.sortDirection to an element. These annotations control whether it shows its key or its text, and how the rows are sorted. Because CustomerName is marked as a text element on the dimension, the front end can show the name next to the ID.
Adding the dimension attributes
The cube exposes the association _Customer. A query can reach through it to bring in an attribute of the dimension, such as the country.
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Net amount by customer and country'
@Analytics.query: true
define view entity ZC_NetAmountByCountry
as select from ZI_SalesItemCube
{
@AnalyticsDetails.query.axis: #ROWS
CustomerId,
@AnalyticsDetails.query.axis: #FREE
_Customer.Country as CustomerCountry,
@AnalyticsDetails.query.axis: #COLUMNS
NetAmount
}The path expression _Customer.Country works because the cube exposes _Customer. If you forget to expose the association in the cube's select list, the query cannot use it.
Common errors
- Selecting from a table. A query on a plain table has no measure semantics and cannot be aggregated. Always select from a cube.
- Missing
@Analytics.query: true. The view activates as an ordinary CDS view. Analytical apps do not list it. - A measure with no axis. The measure is not shown. Put it on
#COLUMNS. - Adding
GROUP BY. It is not needed. The aggregation comes from the cube.
Exercise
A query selects from ZISalesItemCube. You want the country available to the user but not shown when the query first opens. Which annotation value do you set on the country element, and why must the cube expose Customer?
Show answer
Set @AnalyticsDetails.query.axis: #FREE on the country element. The query reaches the country through the path Customer.Country, which only works if the cube lists the association Customer in its select list.