Exercise Objectives
After completing this exercise, you will be able to:
Extract the top n values from a data set by using a Rank node
Understand the impact of the client tool query on the way data is retrieved
Define the rank node so that it adapts dynamically to the selected columns
Business Example
Your company wants to analyze its sales orders by extracting the best-selling products in each country.
You will first create a new calculation view with a rank node that extracts the top 5 products for each country, based on a provided calculation view that retrieves the sales order details from the source tables.
You will then explore additional capabilities of the Rank node.
Overview of Exercise Tasks
Task 1: Create a New Calculation View Using a Rank Node
Task 2: Modify the Calculation View to Better Control Rank Behavior
Task 3: Use the Result Set Type Percentage
Task 4: Use the Sum Aggregation Function
Prerequisites
Prerequisites
Your SAP Business Application Studio development environment should have been set up and the HC300 project imported and deployed.
Task 1: Create a New Calculation View Using a Rank Node
Create a calculation view that will process a top 5 extraction on a provided sales order item view.
Steps
If needed, launch SAP Business Application Studio and start your HCMOD development space.
Preview the data of the calculation view CVCS_SO that will be used as a data source for the rank node. It is located in your resources folder.
In the explorer, expand your HC300_A#### project to display the folder db→src→resources.
Select then right-click the calculation view CVCS_SO and choose Data Preview..
Select the arrow button on the right of the SO_ID column. Then choose Sort ascending.
This calculation view returns information on sales order details (each sales order has several line items; one for each product ID that is part of the order). The dimension Calculation views are used to retrieve, among other things, the Customer Name (BP_COMPANY_NAME) and its country (COUNTRY), as well as the Product ID (PRODUCT_ID) and product category (CATEGORY).
Create a new calculation view of the type Cube, with the following properties:
Field Value Name CVC_SO_RANK Description Sales Orders Rank Data Category CUBE With Star Join [Deselected] In the Explorer tree, choose HC300_A####→db→src and right-click the exercises folder. Choose New File.
Enter CVC_SO_RANK.hdbcalculationview as file name and press Enter.
Enter the calculation view properties as per the table above.
Choose Create.
Result
The calculation view graphical editor opens on the right of your screen.
Add a new Rank node Rank_1 to the calculation view scenario, and define it as the data source for the Aggregation node.
In the Scenario pane, select the Rank node from the palette and drop it to the area below the Aggregation node.
Result
The new node is automatically named Rank_1.Select the Rank_1 node and drag the arrow icon to the Aggregation node.
Define the calculation view resources→CVCS_SO as the data source for the Rank_1 node, and add the following columns to the output:
COUNTRY
PRODUCT_ID
GROSS_AMOUNT
Select the new Rank_1 node and choose + on the right of the node.
In the Find Data Sources window, select the Search field and enter CVCS_SO.
In the search results, select the view HC::CVCS_SO and choose Finish.
In the Mapping tab, select the columns as per the table.
Choose Add To Output.
Define the settings of the Rank node so that it extracts, for each country, the five products that total the biggest sales amount (column: GROSS_AMOUNT).
A rank column, named RANK, should be generated.
Property Value Aggregation Row Result Set Direction Top Result Set Type Absolute Target Value Fixed
Value: 5
Offset Fixed
Value: [Leave Blank]
Generate Rank Column [Selected] Generated rank column name RANK (do not keep the default column name)
Partition column COUNTRY Sort Column GROSS_AMOUNT (direction: Descending) Select the Rank_1 node.
In the Definition tab, define the rank properties using the values from the table.
To add a column to the Logical Partition, choose +. Do the same to define the Sort Column.
In the Aggregation node, add all columns to the output.
Select the Aggregation node.
In the Mapping tab, select the Rank_1 item and choose Add To Output.
Alternatively, you can drag the Rank_1 item from the Data Sources area to the Output Columns area.
Deploy the CVC_SO_RANK calculation view.
Check the deployment status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Preview the data of the CVC_SO_RANK calculation view.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Check the data displayed in the Raw Data tab.
Caution
Because the Rank_1 node is using a CUBE calculation view as a data source, that CUBE calculation view feeds the rank node with a data set on which it applies the defined aggregation. This aggregation is based on the list of attribute columns requested by the query executed on top of the rank node. In the current scenario, the CSCS_SO view has executed an aggregation by COUNTRY and PRODUCT_ID.Filter the data preview to US as the COUNTRY and check that you receive the expected result, which is shown in the following table:
,COUNTRY PRODUCT_ID RANK GROSS_AMOUNT US HT-1037 1 51 352 888.96 US HT-1021 2 8 026 133.60 US HT-1116 3 4 848 000.00 US HT-1106 4 2 441 955.76 US HT-1502 5 1 243 077.12 Choose the arrow at the right of the COUNTRY column header
In the Filter field, enter US and press Enter.
Note
This way of filtering does not re-execute the query against the calculation view, but just filters the current result set. It is valid in the current scenario because the complete result set has 80 rows, which is less than the maximum number of rows (1,000) defined by default in the settings of SAP Business Application Studio (SAP HANA Database Explorer section).
In other scenarios, you might need to apply a filter to the data preview query itself, by choosing
Add Filter. The query will then be re-executed against the calculation view.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE1
Close the preview tab.
Task 2: Modify the Calculation View to Better Control Rank Behavior
You will now modify the properties of the Rank node and output columns of the calculation view CVC_SO_RANK.
Steps
Add the column BP_COMPANY_NAME to the definition of the Rank_1 node and up to the Semantics node.
On the Mapping tab of the Rank_1 node, select the BP_COMPANY_NAME column and choose Add To Output.
Alternatively, you can drag the column from the Data Sources pane to the Output Columns pane.
In the Output Columns pane, right-click the column BP_COMPANY_NAME and choose Propagate to Semantics.
In the confirmation dialog box, choose OK.
Deploy the CVC_SO_RANK calculation view.
Check the deployment status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Preview the data of the CVC_SO_RANK calculation view.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Modify the default SQL query of the data preview to remove the BP_COMPANY_NAME column. You must also remove this column from the GROUP BY clause. Then, execute the modified query and filter the result set on COUNTRY = 'US'.
The modified SQL query should look as follows:
Code Snippet1234567SELECT TOP 1000 "COUNTRY", "PRODUCT_ID", "RANK", SUM("GROSS_AMOUNT") AS "GROSS_AMOUNT" FROM ... GROUP BY "COUNTRY", "PRODUCT_ID", "RANK";In the Data Preview tab, choose
Edit Sql Statement In Sql Console.Remove the column "COMPANY_NAME", both from the SELECT section and the GROUP BY section of the statement.
Do not forget to also remove the commas following the column name.
Choose
Run.Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter US and press Enter.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE2.
Close the preview tabs.
Modify the CVCS_SO_RANK calculation view, adding the BP_COMPANY_NAME column to the Logical Partition of the Rank node. Then, deploy the calculation view.
In the Scenario pane, select the Rank_1 node.
On the Definition tab, under the Partition Column area, choose + and select the BP_COMPANY_NAME column with the drop-down list.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE3.
Preview the data of the CVC_SO_RANK calculation view.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Modify the default SQL query of the data preview to remove the BP_COMPANY_NAME column. You must also remove this column from the GROUP BY clause. Then execute the modified query and filter the result set on COUNTRY = 'US'.
The modified SQL query should look as follows:
Code Snippet1234567SELECT TOP 1000 "COUNTRY", "PRODUCT_ID", "RANK", SUM("GROSS_AMOUNT") AS "GROSS_AMOUNT" FROM ... GROUP BY "COUNTRY", "PRODUCT_ID", "RANK";In the Data Preview tab, choose
Edit Sql Statement In Sql Console.Remove the column "COMPANY_NAME", both from the SELECT section and the GROUP BY section of the statement.
Do not forget to also remove the commas following the column name.
Choose
Run.Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter US and press Enter.
Close the preview tabs.
Modify the view CVC_SO_RANK to activate the Dynamic Partition Elements in the properties of the Rank_1 node. Deploy the calculation view.
In the Scenario pane, select the Rank_1 node.
In the Definition tab, select the Dynamic Partition Elements checkbox.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE4.
Preview the data of the CVC_SO_RANK calculation view. You get the same result, partitioned by COUNTRY and BP_COMPANY_NAME.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Modify the default SQL query of the data preview to remove the BP_COMPANY_NAME column. You must also remove this column from the GROUP BY clause. Then execute the modified query and filter the result set on COUNTRY = 'US'.
The modified SQL query should look as follows:
Code Snippet1234567SELECT TOP 1000 "COUNTRY", "PRODUCT_ID", "RANK", SUM("GROSS_AMOUNT") AS "GROSS_AMOUNT" FROM ... GROUP BY "COUNTRY", "PRODUCT_ID", "RANK";In the Data Preview tab, choose
Edit Sql Statement In Sql Console.Remove the column "COMPANY_NAME",, both from the SELECT section and the GROUP BY section of the statement.
Do not forget to also remove the commas following the column name.
Choose
Run.Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter US and press Enter.
Close all the open tabs in Business Application Studio.
Right-click any open tab and choose Close All.
Task 3: Use the Result Set Type Percentage
Now, you want to retrieve the top 10% best-selling products by country. You will adjust the calculation view accordingly.
Steps
Open the CVCS_SO_RANK calculation view.
In the Explorer view, select the src→exercises→CVCS_SO_RANK calculation view.
In the Rank node definition, remove the column BP_COMPANY_NAME from the output columns. Confirm that it has automatically been removed from the list of columns used to partition the data set.
Double-click the Rank_1 node and display the Mapping tab.
In the Output Columns area, select the BP_COMPANY_NAME column and choose Remove Output Column.
In the confirmation dialog box, read the warning and choose Yes.
In the Definition tab, confirm that the Logical Partition only contains the COUNTRY column.
Adjust the Definition of the Rank node as follows (the table only mentions the properties you need to modify):
Property Value Result Set Type Percentage Target Value Fixed
Value: 0.1
Select the Rank_1 node.
In the Definition tab, adjust the properties as per the table.
Deploy the CVC_SO_RANK calculation view.
Check the deployment status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE5.
Preview the intermediate result set of the Rank_1 node.
In the Scenario pane, right-click the Rank_1 node and choose Data Preview.
Display the Raw Data tab.
Filter the result set on COUNTRY = 'CN'.
Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter CN and press Enter.
Refresh the preview of the CVCS_SO_RANK calculation view.
Choose the Refresh icon in the Raw Data pane.
Close the preview tabs.
In the definition of the CVCS_SO_RANK calculation view, sort the result set of the calculation view by COUNTRY (asc) and RANK (asc).
In the Semantics node, display the Columns tab.
Choose
Sort Result Set.Note
Depending on your screen size and display mode, the button might be hidden. In this case, choose the
Toolbar Overflow button.Choose +Add.
Select the COUNTRY column.
Keep the default sort order Ascending.
Repeat these steps for the RANK column.
Deploy the CVC_SO_RANK calculation view.
Check the build status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE6.
Preview the CVCS_SO_RANK calculation view and confirm that the result set is now logically ordered.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Check that the result set is now ordered in a relevant way.
Close all open tabs except the Calculation View editor tab for the CSC_SO_RANK calculation view.
Select then right-click the tab CVC_SO_RANK.hdbcalculationview and choose Close Others.
Note
You can use the tooltip to check the complete tab name. As an aside, a design-time file always include the file extension —here, .hdbcalculationview– which is not the case for data preview tabs.
Task 4: Use the Sum Aggregation Function
Now, you would like to find the products that sell worst in each country, but this time you want the number of products in the result set for each country to be determined as a percentage of the total sales in this country.
More specifically, you want to extract and rank, in each country, the products that generate the lowest revenue and represent 1% of the total sales (GROSS_AMOUNT) in the country.
Steps
In the CVC_SO_RANK calculation view, adjust the Definition of the Rank node as follows (the table only mentions the properties you need to modify):
Property Value Aggregation Function Sum Target Value Fixed
Value: 0.01
Sort Column GROSS_AMOUNT (direction: Ascending) Note
Keep the Result Set Type setting Percentage.Select the Rank_1 node.
In the Definition tab, adjust the properties as per the table.
Deploy the CVC_SO_RANK calculation view.
Check the deployment status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
Result
Note
The following intermediate solution object is available at this stage:
solutions→CVC_SO_RANK_00_STAGE7
Preview the Calculation View. Check that the result subset for COUNTRY = 'CA' is consistent.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter CA and press Enter.
Confirm that the cumulative rank does not exceed 0.01 for any of the returned rows (in particular the last one).
Close the data preview tab.
(Optional) Consider another way to achieve a similar result.
(Optional) Adjust the Definition of the Rank node as follows (the table only mentions the properties you need to modify).
Property Value Aggregation Function Sum Result Set Direction Down Sort Column GROSS_AMOUNT (direction: Descending) Select the Rank_1 node.
In the Definition tab, adjust the properties as per the table.
(Optional) Deploy the CVC_SO_RANK calculation view.
Check the deployment status after activation.
Choose the Deploy icon for your CVC_SO_RANK calculation view.
In the Console pane, check that the deployment completes successfully.
(Optional) Preview the Calculation View. Compare the results for COUNTRY = 'CA' with what you got before.
Right-click the CVC_SO_RANK calculation view and choose Data Preview.
Display the Raw Data tab.
Choose the arrow at the right of the COUNTRY column header.
In the Filter field, enter CA and press Enter.