Tuesday 11 July 2017

PIVOT Data in HANA

In my previous blog I have covered how to UNPIVOT data in HANA, in this blog I am covering how to PIVOT data in HANA.

While converting MS-SQL procedures into HANA procedures, we came across PIVOT statement in one of the select query, unfortunately HANA SQL Script does not support PIVOT operation hence we had to come up with alternative approach. In this blog I will cover two approaches to PIVOT data in HANA
  1. Using graphical calculation view
  2. Using HANA SQL Script
PIVOT Data using Graphical Calculation View:

Base Table: This is a PO header table which has Employee ID, Vendor ID and PO Number.

SAP HANA Certifications, SAP HANA Tutotials and Materials

PIVOT Output: In pivot output lets say we want to know how many Purchase Orders placed by each employee, as Excel is the best tool to generate pivot output so I generated below output in excel:

SAP HANA Certifications, SAP HANA Tutotials and Materials

We can get same output using graphical calculation view by following below steps:
  • Add a projection node and add base table in it:

SAP HANA Certifications, SAP HANA Tutotials and Materials
  • Since there are 6 different employees in base table hence we need to create 6 calculated fields, one corresponding to each employee and these fields will become our pivot column header.
  • As we need count of PO for each employee we need to put below formula in each calculated field:
SAP HANA Certifications, SAP HANA Tutotials and Materials
SAP HANA Certifications, SAP HANA Tutotials and Materials

As per above expression, if  employee is “E1” then make E1 (new calculated field) = 1 else E1 = 0. Write same expression for all the other fields and change Emp_ID value to E2, E3.., etc.

Note: You need to create calculated fields in Projection node and not on Aggregation node otherwise aggregation on calculated fields will not happen.

Here is the output after creating all the calculated fields:

SAP HANA Certifications, SAP HANA Tutotials and Materials
  • Connect projection node to Aggregation node and activate the view:
SAP HANA Certifications, SAP HANA Tutotials and Materials

Pivoted Output:


In above use case we used Count as aggregation function however if you want to use Sum or Max as aggregated function then you just need to make small change in your calculated field formula and aggregation function in semantic node.

Changes for Sum: Lets say you want to check Sum of all POs placed by an employee, I know logically it does not make sense but I think you won’t mind if I take it as an example. 

SAP HANA Certifications, SAP HANA Tutotials and Materials

Make above change for all the calculated fields.

Output:

SAP HANA Certifications, SAP HANA Tutotials and Materials

Changes for Max: 

Formula remains the same as Sum but you need to change aggregation function in Semantic node:

SAP HANA Certifications, SAP HANA Tutotials and Materials

Output:

SAP HANA Certifications, SAP HANA Tutotials and Materials


PIVOT Data using HANA SQLScript:

There are multiple ways of achieving Pivot functionality using HANA SQLScript

First Option:

Pivot with Count (replace Count with Sum in case you need Sum as aggregated function)

SAP HANA Certifications, SAP HANA Tutotials and Materials

Pasting SQL just in case you want to copy:

select vendor_id, sum(“E1”) AS “E1”, SUM(“E2”) AS “E2”,SUM(“E3”) AS “E3”,SUM(“E4”) AS “E4”,SUM(“E5”) AS “E5”,
SUM(“E6”) AS “E6”
FROM
(
select vendor_id, COUNT(NUM) as “E1”, 0 AS “E2” , 0 AS “E3”, 0 AS “E4”, 0 AS “E5”, 0 AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E1’ GROUP BY vendor_id
union all
select vendor_id, 0 AS “E1”, COUNT(NUM) as “E2”, 0 AS “E3”, 0 AS “E4”, 0 AS “E5”, 0 AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E2’ GROUP BY vendor_id
union all
select vendor_id, 0 AS “E1”, 0 as “E2”, COUNT(NUM) AS “E3”, 0 AS “E4”, 0 AS “E5”, 0 AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E3’ GROUP BY vendor_id
union all
select vendor_id, 0 AS “E1”, 0 as “E2”, 0 AS “E3”, COUNT(NUM) AS “E4”, 0 AS “E5”, 0 AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E4’ GROUP BY vendor_id
union all
select vendor_id, 0 AS “E1”, 0 as “E2”, 0 AS “E3”, 0 AS “E4”, COUNT(NUM) AS “E5”, 0 AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E5’ GROUP BY vendor_id
union all
select vendor_id, 0 AS “E1”, 0 as “E2”, 0 AS “E3”, 0 AS “E4”, 0 AS “E5”, COUNT(NUM) AS “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
WHERE EMP_ID = ‘E6’ GROUP BY vendor_id
) GROUP BY vendor_id ORDER BY vendor_id

Second Option: 

Pivot with Count (replace 1 with Num in case you need Sum as aggregated function)

SAP HANA Certifications, SAP HANA Tutotials and Materials

SQL:

select vendor_id, sum(“E1”) AS “E1”, SUM(“E2”) AS “E2”,SUM(“E3”) AS “E3”,SUM(“E4”) AS “E4”,SUM(“E5”) AS “E5”,
SUM(“E6”) AS “E6”
FROM
(
select vendor_id, CASE (EMP_ID) WHEN ‘E1’
THEN 1
ELSE 0
END as “E1”,
CASE (EMP_ID) WHEN ‘E2’
THEN 1
ELSE 0
END as “E2”,
CASE (EMP_ID) WHEN ‘E3’
THEN 1
ELSE 0
END as “E3”,
CASE (EMP_ID) WHEN ‘E4’
THEN 1
ELSE 0
END as “E4”,
CASE (EMP_ID) WHEN ‘E5’
THEN 1
ELSE 0
END as “E5”,
CASE (EMP_ID) WHEN ‘E6’
THEN 1
ELSE 0
END as “E6”
from “ABHISHEAGRAW”.”abhisheagraw::PIVOT_INITIAL_TABLE”
)GROUP BY vendor_id ORDER BY vendor_id

No comments:

Post a Comment