SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

XS Advanced, Use SQLScript in Calculation Views

Create a Table Function and wrap it in a Graphical Calculation View

Overview

🎓 advanced 25 min. SAP HANAAdvancedBig DataExpress Edition

You will learn

  • โœ”How to implement more complex logics using SQLScript in Table Functions
  • โœ”How to incorporate Table Functions in Graphical Calculation views
  • โœ”Create a replacement to the former Scripted Views in previous versions of SAP HANA
  • โœ”Apply the DENSE_RANK function, to establish the ranking of a row relative to a partition of a dataset
Thomas Jung T Thomas Jung November 1, 2022
Created by September 14, 2018
Contributors

Prerequisites

Prerequisites

Steps

Intro

This tutorial assumes general knowledge of the modeling tool has been acquired through completion of the beginner and intermediate tutorials about calculation views.


Step 1 Create a function
โ€”

In your db/src folder, create a new folder called functions.

Create folder
Create folder

Create a function called SO_RANKING

Function
Function

A template is created for you with different sections

Function
Function

Remove the placeholder for the namespace. Paste the following code into the input section:

SQL
IP_FR_DATE DATE,
IP_TO_DATE DATE,
IP_REGION NVARCHAR(4)

Add the following as a returning table:

SQL
table ( COMPANY_NAME NVARCHAR(80),
                  REGION NVARCHAR(4),
                  SALES DECIMAL(18,2),
                  ORDERS INTEGER,
                  SALES_RANK INTEGER,
                  ORDER_RANK INTEGER  )  

Add the following code between BEGIN and END:

SQL

return

SELECT   company_name, region, sales, orders, sales_rank, order_rank from(  
  select
	 T2."COMPANYNAME" as COMPANY_NAME,
	 T3."REGION" as REGION,
	 sum(T1."NETAMOUNT") as SALES,
	 count(T0."SALESORDERID") as ORDERS,
	 dense_rank() over ( order by sum(T1."NETAMOUNT") desc ) as sales_rank,
	 dense_rank() over ( order by count(T0."SALESORDERID") desc ) as order_rank
	 from "SO.Header" T0
     inner join "SO.Item" T1 on T0."SALESORDERID" = T1."HEADER.SALESORDERID"
     inner join "MD.BusinessPartner" T2 on T0."PARTNER.PARTNERID" = T2."PARTNERID"
     inner join "MD.Addresses" T3 on T2."ADDRESSES.ADDRESSID" = T3."ADDRESSID"  
     where TO_DATE(T1."DELIVERYDATE") between :IP_FR_DATE and :IP_TO_DATE
       and T3."REGION" = :IP_REGION
     group by T2."COMPANYNAME", T3."REGION"
     order by sales_rank, T2."COMPANYNAME"
     )
     where sales_rank < 11;

Save and build the function. This is what it should look like

Function
Function

For more information about DENSE_RANK() and other function modules, visit the SAP HANA and System Views Reference

Step 2 Test the function
+
Step 3 Create a Calculation View
+
Step 4 Map parameters
+
Step 5 Test the Calculation View
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 5
1. Create a function 2. Test the function 3. Create a Calculation View 4. Map parameters 5. Test the Calculation View

Learn more →