Vous êtes sur la page 1sur 51

SAP HANA

modeling
SANSO TECHNOLOGIES
Revisit to Calculation Views

 Calculation views are composite views and can be used to


combine other views. It can consume other Analytical, Attribute,
other Calculation Views & tables.
It can perform complex calculations not possible with other views.

Calculation view can be defined as either graphical views or


scripted views depending on how they are created.
Graphical views can be modelled using the graphical modeling
features of the SAP HANA Modeler. Scripted views are created as
sequences of SQL statements. In this chapter we will create a simple
calculation view which will be used further to explain other features
like
 • Variables and Input Parameters
 • Analytic Privileges
Example
KPI

 We need to find out


 • What are the sales value for "Asia" region?
 • What are the total sales value for "Shirts"?
 • What are the total sales value for "Europe" for "Jackets"?
 Let us create a graphical calculation view to get answer of the
above mentioned questions.
Schema

 Create new tables in SAP HANA and fill them with data.
 create schema <schema_name>;
 ----REPLACE <YOUR SCHEMA> WITH YOUR SCHEMA NAME
Product Table

 ----REPLACE <YOUR SCHEMA> WITH YOUR SCHEMA NAME

---- Create Product table


create column table "<YOUR SCHEMA>"."PRODUCT"(
"PRODUCT_ID" INTEGER,
"PRODUCT_NAME" VARCHAR (100),
primary key ("PRODUCT_ID")
);

insert into "<YOUR SCHEMA>"."PRODUCT" values(1,'Shirts');


insert into "<YOUR SCHEMA>"."PRODUCT" values(2,'Jackets');
insert into "<YOUR SCHEMA>"."PRODUCT" values(3,'Trousers');
insert into "<YOUR SCHEMA>"."PRODUCT" values(4,'Coats');
insert into "<YOUR SCHEMA>"."PRODUCT" values(5,'Purse');
Region Table

 -- Create Region table


create column table "<YOUR SCHEMA>"."REGION"(
"REGION_ID" INTEGER,
"REGION_NAME" VARCHAR (100),
"SUB_REGION_NAME" VARCHAR (100),
PRIMARY KEY ("REGION_ID")
);
insert into "<YOUR SCHEMA>"."REGION" values(100,'Americas','North-
America');
insert into "<YOUR SCHEMA>"."REGION" values(200,'Americas','South-
America');
insert into "<YOUR SCHEMA>"."REGION" values(300,'Asia','India');
insert into "<YOUR SCHEMA>"."REGION" values(400,'Asia','Japan');
insert into "<YOUR SCHEMA>"."REGION" values(500,'Europe','Germany');
Sales Table

 -- Create Sales table


create column table "<YOUR SCHEMA>"."SALES"(
"REGION_ID" INTEGER ,
"PRODUCT_ID" INTEGER ,
"SALES_AMOUNT" DOUBLE, PRIMARY KEY ("REGION_ID",
"PRODUCT_ID") );
insert into "<YOUR SCHEMA>"."SALES" values(100,1,100);
insert into "<YOUR SCHEMA>"."SALES" values(100,2,90);
insert into "<YOUR SCHEMA>"."SALES" values(100,5,85);
insert into "<YOUR SCHEMA>"."SALES" values(200,2,80);
insert into "<YOUR SCHEMA>"."SALES" values(200,1,75);
insert into "<YOUR SCHEMA>"."SALES" values(300,3,85);
insert into "<YOUR SCHEMA>"."SALES" values(400,4,75);
insert into "<YOUR SCHEMA>"."SALES" values(500,1,65);
insert into "<YOUR SCHEMA>"."SALES" values(500,2,65);
Grant rights

 GRANT SELECT ON SCHEMA <YOUR SCHEMA> TO _SYS_REPO WITH


GRANT OPTION;
Model graphical view

 Model the graphical view based on above mentioned KPI’s


Variables

 Sometimes we need to filter the data based on user's input. This is


where Input Paramters and Variables come into the pictures.
Input parameters and variables are used to filter the result of
modeling views.
Introduction to variables

 Variables are bound to columns and are used for filtering using
WHERE clauses. As such, they can only contain the values available
in the Columns they relate to.

Sounds confusing?? Let us take a simple example.


In the previous chapter, we created a calculation view which has 4
columns PRODUCT_NAME, REGION_NAME, SUB_REGION_NAME and
SALES_AMOUNT.
REGION_NAME contains different regions like “America”, “Asia” and
“Europe”. The calculation view gives all the region’s details in
output.
 Now suppose we want to filter the data on region basis. The
calculation view will prompt the user to specify Region Name and
the output data will be filtered based on that.
For example if the user provides the value as “Asia”, the calculation
view will show data only for “Asia” region.
Important features of variables

 • You use variables to filter data at runtime. You assign values to


these variables by entering the value manually, or by selecting it
from the drop-down list.
 • You can also define whether the Variable is Mandatory or if it
should have a Default Value.
 • You can apply variables only in analytic and calculation views.
Variable Types

 The following types of Variables are supported:

 Single Value: Use this to apply a filter to a Single Value.


 Interval: Use this where you want the user to specify a set start and end
to a selected Interval.
 Range: Use this when you want the end user to be able to use operators
such as “Greater Than” or “Less Than”.

Input Parameters

 Sometimes you might not want a variable to just restrict the data of a
view. But you also want to take input from the user and process it,
returning dynamic data based on the user selection.
Input Parameters makes this possible.
 Let us consider the same calculation view we created before which has
following attributes and measures.
• Attributes: PRODUCT_NAME, REGION_ID, REGION_NAME,
SUB_REGION_NAME
• Measures: SALES_AMOUNT
Suppose we want the end user to decide whether SALES_AMOUNT or
NET_AMOUNT should be shown in the output.
We can create a input parameter of type “Static List” which will have 2
values “SalesAmount” and “NetAmount”.
The calculation view will prompt for the input parameter. The user will
choose either “SalesAmount” or “NetAmount”. Based on this selection,
the output will be shown.
Important Features of Input
Parameters
 • Input Parameters can contain any value the reporting user has to
enter to parameterize the result. Therefore, a data type must be
specified for each Input Parameter.
 • Input Parameters are passed by Placeholders and used in
Formulas.
Input Parameter Types

 The following types of Input variables are supported.


• Currency:
Use this during currency conversion where the end user should specify a source
or target currency.
• Date:
Use this to retrieve a date from the end user using a calendar type input box.
• Static List:
Use this when the end user should have a set list of values to choose from.
• Attribute Value:
When an Input Variable has this type, it serves the same purpose as a normal
Variable.
• None:
If none of the above applies you do not have to specify an Input Variable type.
The Type can be left blank.
How to Pass Input Parameters and
Variables in SQL Query
 Open the data preview of calculation view as mentioned in
previous step.
Click on the “Show Log” and then double click on the message
highlighted below. A new window will be opened which contains
the SQL query.
 Input Parameter is passed using PLACEHOLDER:

The value of Input Parameter is passed using PLACEHOLDER clause.


('PLACEHOLDER' = ('$$DISCOUNT$$', '10'))

WHERE:
The value of Variable is passed using WHERE clause.
WHERE ("REGION_NAME" IN ('Asia') )
Joins – Different Types of Joins in
SAP HANA
 A Join clause combines records from two or more tables /view in a
database.
SAP HANA supports following types of join.
 • Inner Join
 • Left Outer Join
 • Right Outer Join
 • Full Outer Join
 • Referential Join
 • Text Join
 We are going to use 2 tables to explain the different types of Join.
CUSTOMER and SALES_ORDER.
Customer Table

 -- REPLACE <Schema_Name> WITH YOUR SCHEMA

CREATE COLUMN TABLE <Schema_Name>."CUSTOMER" (


"CustomerID" nvarchar(10) primary key,
"CustomerName" nvarchar(50)
);
INSERT INTO <Schema_Name>."CUSTOMER" VALUES ('C1', 'Alfred');
INSERT INTO <Schema_Name>."CUSTOMER" VALUES ('C2', 'John');
INSERT INTO <Schema_Name>."CUSTOMER" VALUES ('C3', 'Maria');
INSERT INTO <Schema_Name>."CUSTOMER" VALUES ('C4', 'Harry');
Sales Order Table

 CREATE COLUMN TABLE <Schema_Name>."SALES_ORDER" (


"OrderID" integer primary key,
"CustomerID" nvarchar(10),
"Product" nvarchar(20),
"Total_Units" integer
);
INSERT INTO <Schema_Name>."SALES_ORDER" VALUES (101,
'C1','Camera',300);
INSERT INTO <Schema_Name>."SALES_ORDER" VALUES (102,
'C1','Mobile',200);
INSERT INTO <Schema_Name>."SALES_ORDER" VALUES (103,
'C2','iPod',500);
INSERT INTO <Schema_Name>."SALES_ORDER" VALUES (104,
'C3','Television',400);
INSERT INTO <Schema_Name>."SALES_ORDER" VALUES (105,
'C5','Laptop',800);
Inner Join

 The INNER JOIN selects the set of records that match in both the
Tables.
Where to Use

 Inner join should be used if referential integrity is ensured.


Inner Join is much faster that Outer Join thus is the preferred solution
if possible from semantical perspective
Attribute View: Inner Joins can be used to join different master data
tables to a joint dimension Analytical Views: Inner Joins can be used
if referential integrity cannot be ensured

Left Outer Join

 The Left Outer Join selects the complete set of records from first
table (CUSTOMER), with the matching records (where available) in
second table (SALES_ORDER). If there is no match, the right side will
contain null.
Right Outer Join

 The Right Outer Join selects the complete set of records from
second table (SALES_ORDER), with the matching records (where
available) in first table (CUSTOMER). If there is no match, the left side
will contain null.
Full Outer Join

 The INNER JOIN selects the set of records that match in both the
Tables.
SAP HANA Referential Join

 Referential Join is semantically an inner join that assume that


referential integrity is given.

Note: Referential integrity is the property of database which ensures


that each foreign key value in a table exists as a primary key in the
referenced table.

Referential join is performance wise better than inner join, but only
be used when you are sure that referential integrity is maintained.
SAP HANA Text Join

 Text Join is used in order to get language-specific data.

You have a product table that contains product IDs without


descriptions and you have a text table for products that contains
language-specific descriptions for each product. You can create a
text join between the two tables to get the language-specific
details. In a text join, the right table should be the text table and it is
mandatory to specify the Language Column.
Text Joins in SAP HANA

 Text Join is used in order to get language-specific data.


 Text Tables
 Table A is a text table of table B if the key of A comprises the key of
B and an additional language key field (field of data type LANG).
Table A may therefore contain explanatory text in several
languages for each key entry of B.
Implementation of Text Join in SAP
HANA
 Text Join is used to fetch the description based on user's session
language. Once we implement the text join in SAP HANA, it
automatically find out user's language and give description in that
language.
 Example:
Suppose there is a table called "PRODUCT" which contains Product
ID and Product Name.
There is also a text table of "PRODUCT", which is called
"PRODUCT_TEXT_TABLE". The text table contains the description of
products in different language. For example "Cotton Shirts" in English
and "Baumwoll-Shirts" in German.
Implementation

 Purpose:
Create a calculation view which will give Product Name and
Product Description. The Product Description should be only in user's
session language.

Implementation:

 Open HANA Studio. Right click on your schema and open SQL Editor.
Copy the below SQL script to create 2 tables - "PRODUCT" and
"PRODUCT_TEXT_TABLE".
Product Table

 -- REPLACE <YOUR_SCHEMA_NAME> WITH YOUR ACTUAL SCHEMA NAME

-- PRODUCT table
CREATE COLUMN TABLE "<YOUR_SCHEMA_NAME>"."PRODUCT"(
"PRODUCT_ID" INTEGER ,
"PRODUCT_NAME" VARCHAR(20) ,
primary key ("PRODUCT_ID"));
 insert into "<YOUR_SCHEMA_NAME>"."PRODUCT" values(1,'Shirts');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT" values(2,'Jackets');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT" values(3,'Trousers');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT" values(4,'Coats');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT" values(5,'Purse');
Product Text

 -- PRODUCT text table


CREATE COLUMN TABLE "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE"(
"PRODUCT_ID" INTEGER ,
"LANGUAGE" VARCHAR(1),
"PRODUCT_DESCRIPTION" VARCHAR(50) ,
primary key ("PRODUCT_ID", "LANGUAGE"));
 insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(1,'E', 'Cotton Shirts');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(1,'D', 'Baumwoll-Shirts');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(2,'E', 'Leather jacket');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(2,'D', 'Lederjacke');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(3,'E', 'Trousers and Pants');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(3,'D', 'Hosen und Hosen');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(4,'E', 'Coats and Blazers');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(4,'D', 'Mäntel und Jacken');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(5,'E', 'Purse and Handbags');
insert into "<YOUR_SCHEMA_NAME>"."PRODUCT_TEXT_TABLE" values(5,'D', 'Geldbörse und
Handtaschen');
SAP HANA Engines
 SAP HANA has mainly 3 types of engines that are used based on the
views required by the model.

• Join Engine:
 • Used when querying an Attribute View
 • The join engine is also used, when you run plain SQL.

• OLAP Engine:
 • Used for Analytic view (without calculated columns).

• Calculation Engine:
 • Used for Analytic views with calculated attributes and Calculation views
Analytic Privileges – Overview and
Importance
 Analytic privileges control access to SAP HANA data models.
Analytic privileges are used to grant different users access to
different portions of data in the same view depending on their
business role. It allows us to maintain row-level access.
Why do we need Analytic Privilege

 SQL privileges implement authorization at object level only. Users


either have access to an object, such as a table, view or
procedure, or they do not.

While this is often sufficient, there are cases when access to data in
an object depends on certain values or combinations of values.
Analytic privileges are used in the SAP HANA database to provide
such fine-grained control of which data individual users can see
within the same view.
 Suppose there is a calculation view which contains the sales data of
all the regions like Asia, Europe and America.

The regional managers must have access to the calculation view to


see the data. However, managers should only see the data for their
region. The manager of America region should not be able to see
data of other region.

In this case, an analytic privilege could be modeled so that they


can all query the view, but only the data that each user is
authorized to see is returned.
Important Facts about Analytic
Privileges
 • Analytic privileges are intended to control read-only access to
SAP HANA information models, that is
 • Attribute views
 • Analytic views
 • Calculation views

 • Analytic privileges do not apply to database tables or views


modeled on row-store tables.
System Generated Schemas in
HANA
 There are 3 types of schemas.
 User Defined Schema
 System Defined Schema
 SLT Derived Schema
 User Defined Schema: These are created by user (DBA or System
Administrator)
SLT Derived Schema: When SLT is configured, it creates schema in HANA
system. All the tables replicated into HANA system are contained in this
schema
System Defined Schema:These schemas are delivered with the SAP
HANA database and contains HANA system information. There are
system schemas like _SYS_BIC, _SYS_BI, _SYS_REPO, _SYS_STATISTICS etc.

System Schemas

 _SYS_BIC:
This schema contains all the columns views of activated objects. When the user activates the
Attribute View/Analytic View/Calculation View/Analytic Privilege /Procedure, the respective run-
time objects are created under _SYS_BIC/ Column Views.
_SYS_REPO:
Whatever the objects are there in the system is available in repository. This schema contains the
list of Activated objects, Inactive Objects, Package details and Runtime Objects information etc.
Also _SYS_REPO user must have SELECT privilege with grant option on the data schama.
 _SYS_BI:
This schema stores all the metadata of created column Views. It contains the tables for created
Variables, Time Data (Fiscal, Gregorian), Schema Mapping and Content Mapping tables.
_SYS_STATISTICS:
This schema contains all the system configurations and parameters.
_SYS_XS:
This schema is used for SAP HANA Extended Application Services.
What happens when we activate
the modeling view
 Whenever we create a modeling view (Attribute View, Analytic
View, and Calculation View) and activate it, the runtime column
views are created and stored under column view section of
_SYS_BIC schema.
 The modeling view (for example a calculation view) cannot be
accessed directly. We need to call the corresponding column view
in _SYS_BIC.
Let us have a closer look.
 • Right click on a modeling view and click on Data Preview. Go to Raw
tab to see the output.
 Click on the Show Log Button on top right corner and copy the SQL
query.
 Paste this SQL query in a SQL editor and let us analyze.
This is actually calling the corresponding runtime object (column
view) from _SYS_BIC schema.

Vous aimerez peut-être aussi