SAP HANA Scripted Calculation View
Hello Everyone, When I was trying to learn HANA scripted calculation view, I had to spend lot of time in creating tables, views and data records in order to get my hands dirty and learn how the scripted calculation views works. What I am trying to do here is gather all of these information in this blog so that one can create their own tables, data and finally scripted calculation Views in the following ways.
We will Discuss Calculation View using.
1. SQL Script – Using CE Functions
2. Table Functions
There is a basic creation of graphical calculation view shown here in saphanatutorial.com. Please use this link to build the tables REGION, SALES and PRODUCT. Also create records into these tables using the same link. This is a wonderful site to learn HANA from. Here is an overview of how the graphical calculation view will look like.
Please note here we will not learn how to create the above shown graphical view but learn how to create this calcualtion view using SQL script.
1. SQL Script – Using CE Functions
Step1: Create a new calculation view of the type script as shown below.
Step 2: Here start writing the SQL script code. We will use CE functions here. I will not spend time in explaining what each CE function does. It has been wonderfully described here. Please refer that. I will show below how the code looks like. Also make sure you have created Columns on the right hand side for the VAR_OUT to export the tabular data. Please see below.
Please find the code below to try it yourself.
/********* Begin Procedure Script ************/
region = CE_COLUMN_TABLE(“HANA_TUTORIAL”.“REGION”,
[ “REGION_ID” ,
sales = CE_COLUMN_TABLE(“HANA_TUTORIAL”.“SALES”,
product = CE_COLUMN_TABLE(“HANA_TUTORIAL”.“PRODUCT”,
join1 = CE_JOIN(:region, :sales,
join2 = CE_JOIN(:join1, :product,
p_out = CE_PROJECTION(:join2,
CE_CALC(‘”SALES_AMOUNT”*0.3’,DOUBLE) as “MARGIN” ]);
/********* End Procedure Script ************/
2. SQL Script – Using Table Functions
Step 1: Please go to developement Perspective first. Choose the tab ‘Repository’ and right click on the content package and ‘Import remote workspace’.
Step 2: Rightclick on the package ‘New->Other’ Search for and choose ‘Table Function’. Click next and Put the name as shown below
Step 3: Write your code.
FUNCTION “SYSTEM”.“P1942191456::TBF_GET_SALES_DATA” ( )
RETURNS TABLE (
“SALES_AMOUNT” double, “MARGIN” double )
SQL SECURITY INVOKER AS
Write your function logic
RETURN select b.“PRODUCT_ID”, c.“PRODUCT_NAME”, a.“REGION_ID”, a.“SUB_REGION_NAME”, b.“SALES_AMOUNT”,
get_margin(b.“SALES_AMOUNT”) as “MARGIN”
“HANA_TUTORIAL”.“REGION” as a
“HANA_TUTORIAL”.“SALES” as b
on a.“REGION_ID” = b.“REGION_ID”
inner Join “HANA_TUTORIAL”.“PRODUCT” as c
on b.“PRODUCT_ID” = c.“PRODUCT_ID”;
I have added a scaler user defined function called ‘GET_MARGIN’ in the above code. You can refer to how to build a scaler user defined functions in this wonderful blog written by Rich Heilman here. Please find the code below.
/* Begin of Code
create function get_margin(im_var1 double)
returns result double
SQL SECURITY INVOKER as
result := :im_var1 * 0.3;
/* End of Code
Step 4: Now create a claculation view of the type graphical. Put the name and description. Keep everything as default and in the ‘Aggregation’ node click in add an object as you normally do. Here you will find the Table function you have just created as a table. Please see below
You have now added the table function like you do for tables or views. Add the fields to output and in the ‘Semantics’ you can choose ‘SALES_AMOUNT’ and ‘MARGIN’ as measures and rest as attributes.
3. SQL Script – Procedure
Step 1: Create a procedure similarly as we did in SQL script using CE Functions in the biginning of this blog. Under Catalogue->your_schema->procedure, right click and create a new procedure. Also create the output parameters, let’s say P_OUT and make sure it has all the fields you would want in the final CE_PROJECTION function. In turn structure of P_OUT should be equivalent to structure of VAR_OUT in the final scripted calculation view. This will get clearer in the next step.
The above code is an exact copy as shown in ‘SQL Script – Using CE Functions’ above
Step 2: Create a new Calculation View of the type script. And in the script area put the code as shown below. Also donot forget to create the output structure. It is shown in the below image on the right hand side under Output. This structure is the ‘Var_Out’ structure and is equivalent to the P_OUT we had created in the above step in the procedure. (Assign the correct datatypes for the fields). Lastly go to schema ‘ _SYS_BIC -> Procedure’ and drag the Procedure in the scripting workbench, as shown below.
Finally the output for each calculation views will be the same as we tried to replicate the same output using different methods. This is the Analysis view.
thx for sharing your experiences.
Maybe some hints for your future developments:
Thanks for your comments and added information. I am sharing the basic steps and concept.
Thank you Anik Sircar for the information. It is a in a detailed manner.
Hi, thank you for sharing your knowledge with the mankind.
I have a question. How can I create a temporary table in a SQLScript Calculation View?