You are on page 1of 7

Calculated Column in SAP HANA

HANA 4,222 Views

inShare8

Hello Everyone,
The typical enterprise data warehouse contains three layer system with traditional RDBMS
as Database. The layers are Database, Application/Processing and Reporting.

Database Layer: This is where all the data is being stored. The limitation with traditional
database is, It is similar to hard disk we have in our laptops, which means we can only
perform read and writes on it but not calculations.

Application/Processing Layer: This layer takes care of all the data processing after data
has been read from Database layer. This layer has to be run in a system with higer RAM to
make sure the processing happens fast based on the size of the organization data.

Reporting/Business Layer: This where business/users starts looking/analysing data


present in the organiztion data warehouse.
Now lets say if user executes a particular report which has to fetch subset of data from huge
dataset residing in database. All the calculations will be residing either in application or in
business layer because of the limitation with database layer. If the subset of data required
for reporting is a filter on one calculation happens at application layer, then the entire data
has to transfer from database layer to application layer because system doesnt know the
result dataset until the calculation is performed.
In this type of landscape, we have two challenges

Data transfer between layers takes time and reading from traditional/disk databases
takes time.

Unnessarily moving lage data sets between layers.


We can overcome the first challenge with SAP HANA in place, because HANA stores entire
data in memory which means that there is no need for data transfer between Database and
Application Layers. Another advantage we have here is, as HANA stores everything in RAM
we can perform all the calculations at database layer itself instead of application layer.

With the new features like, Calculated Column (Attribute/Measure), Restricted Measure,
Input Parameters and variables exclusively available in SAP HANA, we can push most of the
logics residing in Application and Business layers to HANA. With this we will be able to take
out the burden on Application and Reporting layers, decreasing the time of execution.
In this article I would like to explain about Calculated Column which is one of the key
feature available in HANA. Also I will be explaining what are the different functions available
to create Calculated columns and syntax for each.
What is Calculated Column?
It is an additional column which can be created by us, the results of which are calculated at
runtime based on the existing column and using either functions, input parameters and
constants.
Now lets create an Attribute View in sap-student package on PRODCUTS table from
SAP_STUDENT schema. To create a new Attribute View, right click on package sap-student
New Attribute View.

Please go through article SAP HANA How to create Attribute View for detailed explanation
on Attribute View creation.
Now we have Attribute View AT_PRODUCTS created on PRODUCTS table and below screen
shows the data we have in it.

Proceed to the next page to continue reading


If we look at the data, we have Product Code which is the key column. The key column will
be used to join the attribute view with any facts either in Analytic View or Calculation View,
however the values in it here are alpha-numeric i.e. a NVARCHAR datatype.

One of the best practice in HANA modeling is to define joins on columns with either
INTEGER or BIGINT as data types. It is not recommended to have joins on columns with
NVARCHAR or DECIMAL as data types, what might create performance issues.
In order to follow best practice we will see how we can use calculated column to extract
integer value from existing alpha-numeric PRODUCT_CODE column.
Also if business wants to see how much percentage of discount they are offering on each
product. We should be able to calculate this with the help of BUYPRICE and MSRP column,
with the help of calculated column.
In this exercise we are going to create calculated columns on

PRODUCT_CODE to convert the alpha-numeric values to integers by removing S and


_ values.

BUYPRICE and MSRP to calculate Discount percentage using formula (MSRPBUYPRICE)*100/MSRP


Lets go ahead and create a new calculated column PRODUCT_NUMBER by right clicking on
Calculated Columns folder New in Attribute View data foundation section.

Here we can see various functions available categorized into different sections like

Conversion Functions

String Functions

Mathematical Functions

Date Functions

Misc Functions

Along with these functions we also have basic operators listed in operators sections. The
source for the calculated column can be the columns coming from source tables or another
calculated column.
In our case to make PRODUCT_CODE value to number, we should able to remove charaters
S and _. The below formula gives the exact result as we need. The functions used in this
formula are instr, midstr and concatenation (+ symbol can be used for concatenation).
midstr(PRODUCTCODE,2,instr(PRODUCTCODE,_)-2) +
midstr(PRODUCTCODE,instr(PRODUCTCODE,_)+1,strlen(PRODUCTCODE))

In the same way we can create another calculated column for Discount Percentage using
the basic operators.
Lets create new calculated column and name it as DISCOUNT_PER and data type as
DECIMAL(5,2). The formula used here is
(MSRP BUYPRICE)*100/MSRP
This gives the exact results as we expect. The below screen shows us the result data
preview of the attribute view.

By this we can say that calculated columns are very useful in SAP HANA and has good
amount of functions that can be used to build the same. If we need any help on the syntax
of the function or how different functions works, we can use the symbol in calculated
column editor screen or refer the below sap link for that.
http://help.sap.com/saphelp_hanaplatform/helpdata/en/d7/70825bbb5710148ca7cd47cb08
6923/content.htm
Thus we have seen the advantages of having calculation ability at database layer and how
to create calculated columns in Attribute View.
Thank you for reading and hope this information is helpful. Please do share with your friends
if you feel the information is useful.
Happy Learning.