Wednesday, 6 July 2016

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

INTRODUCTION

STANDARD HIERARCHY

Definition:
Standard hierarchy is a tree hierarchy which is used to organize business processes of a controlling area. The highest node of a standard hierarchy is normally the first business process group. The groups created thereafter make of the remaining nodes of the standard hierarchy.
The standard hierarchy is assigned directly to the controlling area and has itself a set of business process groups assigned to it. This ensures that all business processes belonging to a controlling area are grouped together.

Example:

Sales Hierarchy

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Root Root is at the top of Hierarchy, It doesn’t have superior Nodes.
Nodes The upper-most node is the root. Each node has exactly one parent node (predecessor) Nodes that can be posted to refer to the hierarchy basic characteristic. Nodes that cannot be posted either text nodes or external characteristic nodes.
Leaves A node without lower-level nodes (successors). Consist of characteristic values for the basic characteristic.
Hierarchy Levels The level of the node corresponds to the number of its predecessors increasing up to the root of one All nodes on the same level of the hierarchy (nodes that are the same distance away from the root) form a hierarchy level. The root equals level 1.
Intervals Set of leaves that are described by its upper and lower boundaries. Can be created if a node has more than one leaf.


STANDARD HIEARCHY IN SAP

There are different standard hierarchies available in SAP ECC like cost center, profit center, cost element etc.
There are three tables in SAP for groups in standard hierarchy.
  • SETHEADER
  • SETNODE
  • SETLEAF
SETHEADER holds the top nodes of the groups with SETTYPE either as S or B. If SETTYPE is S, then the top node has sub groups. Then take the SETNAME from SETHEADER and filter SETNODE table with SETNAME.

To build a standard hierarchy, we have to read SETNODE for each level of the hierarchy and build the tree.

In SAP, there are many different groups such as Cost Center Group, Cost Element Group, and Profit Center Group etc. Each and every group is identified with unique code in field SETCLASS (ex: 0106 for Profit Center, 0101 for Cost Center and 0102 for Cost Element Groups etc.). If the SETTYPE is B, then the top node have reached the last sub group and with that SETNAME, filter SETLEAF with SETCLASS, SUBCLASS and SETNAME.

The following is one of the examples of how a standard hierarchy looks in SAP ECC. This hierarchy is a cost center hierarchy with group (root) node C1000 and controlling area 0001.

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

The details of the above cost center hierarchy are provided below:

CHARACTERISTICSDETAILS
Controlling Area0001
RootC1000
NodesC1010, C1110, C1120, C1200, C1300, C1400
Hierarchy LevelsHighest Level – 4 (1234, 4568, 98763)
Group NodesC1000, C1010, C1110, C1120, C1200, C1300, C1400, C1210, C1220, C1230, C1310, C1320, C1410, C1420, C1430, C1440, C1450
Cost Center (KOSTL)1234, 4568, 98763

NOTE: Either group node or cost center will be a leaf node.
The text in the above hierarchy is description of the nodes obtained from two text tables i.e. SETHEADERT for group nodes (for all hierarchies) and CSKT for cost center (specific to cost center hierarchy).
This cost center hierarchy will be used to explain implementation of standard hierarchy in SAP HANA.

IMPLEMENTATION OF STANDARD HIERARCHIES IN SAP HANA

In ECC, there are several function modules which can be used to implement as well as display standard hierarchies. But in SAP HANA there is no Standard way to create and view this type of hierarchy (like simple parent-child and level hierarchies available).
Here the above shown cost center hierarchy will be used to explain implementation and displaying a standard hierarchy in SAP HANA.
Here group (root) node and controlling area will be required initially; hence SETHEADER table will not be used. So, only SETNODE and SETLEAF tables, group (root) node, controlling area, and CSKS (cost center), CSKT (cost center text) and SETHEADERT (group nodes text) tables will be required.
The below steps will be followed for implementation of this hierarchy:
  • Create a scripted calculation view to get parent child relation of the hierarchy using, SETNODE, SETLEAF and the module table i.e. CSKS for cost center, CSKA for cost element, CEPC for profit center etc. As in this case, we are implementing cost center hierarchy, so CSKS table will be used.
  • To get the description texts, join the text tables to the above created view. The text tables SETHEADERT for group nodes and specific text tables for the module like CSKT for cost center, CEPCT for profit center text, etc. will be used. As in this case, we are implementing cost center hierarchy, so CSKT table will be used.
  • Flatten the above parent child-relation view (with texts/description) to get level wise data/hierarchy.

STEP 1: CREATE A SCRIPTED CALCULATION VIEW TO GET PARENT-CHILD RELATION

The code below will be used to implement the above given cost center hierarchy as a parent child relation in a scripted calculation view.
Note: By doing some changes you can implement this code for other FI/CO hierarchies like cost element, profit center etc., doing some minor changes in the code. (Example: for profit center, use CEPC table will be used instead of CSKS and changing some key fields like PRCTR instead of KOSTL, and some basic conditions will be changed like SETCLASS = 0106, instead of 0101)
The following are the details of the scripted calculation view:

NAMETYPEPARAMETER CASE SENSITIVE
CA_HIERARCHYScriptedTrue

Input Parameters:

NAMETYPESIZECOMMENTS
ControllingAreaNVARCHAR12to input controlling area of the hierarchy
Group_Root_NodeNVARCHAR24to input the group node (root node)

Output Columns:

NAMETYPESIZECOMMENTS
MANDTNVARCHAR3Client
PARENTNVARCHAR24Parent group node
GROUP_NODENVARCHAR24Group Node (it can also be a leaf node)
COST_CENTERNVARCHAR24Cost Center (this can only be a leaf node)
LEVELINTEGER
-
Level of the group node/cost center (for root node, LEV = 1)
CONTROLLING_AREANVARCHAR4Hierarchy controlling area (it will be same as the input controlling area)

Conditions:

CONDITONDETAILS
SETCLASS = ‘0101’This condition will be same for all cost center hierarchies (for profit center- 0106, for cost element – 0102, etc.)


Source Code:

/********* Begin Procedure Script ************/

/* Compute the Cost Center Standard Hierarchy */

NUM int := 0;
LEV int := 1;

BEGIN

/*In case Group_Root_Node is empty, retrieve only leaf(Cost center) node from SETLEAF table*/

if :Group_Root_Node = '' then

LT_CC_TMP = SELECT MANDT AS MANDT, NULL AS PARENT, NULL AS GROUP_NODE, VALOPTION AS VALOPTION, VALFROM as VALFROM, VALTO as VALTO, 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM SETLEAF
WHERE SETCLASS = '0101' and SUBCLASS = :ControllingArea;
                                                
var_out = SELECT MANDT, PARENT, GROUP_NODE, VALFROM AS COST_CENTER, LEVEL, CONTROLLING_AREA
FROM :LT_CC_TMP;
                                
LT_CC_TMP2 = SELECT CC.MANDT AS MANDT, NULL AS PARENT, CC.GROUP_NODE AS GROUP_NODE, CSKS.KOSTL AS COST_CENTER, CC.LEVEL AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM :LT_CC_TMP as CC inner join CSKS as CSKS ON (CSKS.MANDT = CC.MANDT and CSKS.KOSTL >= CC.VALFROM and CSKS.KOSTL <= CC.VALTO)
WHERE CSKS.KOKRS = :ControllingArea;
                                                       
var_out = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :var_out
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_CC_TMP2;


/*If Group_Root_Node is empty, empty cost center should also be considered*/

LT_CC_TMP_2 = SELECT DISTINCT MANDT AS MANDT, NULL AS PARENT, NULL AS GROUP_NODE, '' AS COST_CENTER, 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM SETLEAF
WHERE SETCLASS = '0101' and SUBCLASS = :ControllingArea;

var_out = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :var_out
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_CC_TMP_2;


/*Retrieve all hierarchy groups and the cost center from both SETNODE and SETLEAF, incase Group_Root_Node is not empty*/

else

LT_ROOT = SELECT DISTINCT MANDT AS MANDT, NULL AS PARENT, SETNAME AS GROUP_NODE, NULL AS COST_CENTER, 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM SETNODE
WHERE SETCLASS = '0101' and SUBCLASS = :ControllingArea and SETNAME = :Group_Root_Node;

/*inacase for a group node, there is no subgroup, then it will not be in SETNODE, but only in SETLEAF*/

LT_ROOT2 = SELECT DISTINCT MANDT AS MANDT,  NULL AS PARENT, SETNAME AS GROUP_NODE, NULL AS COST_CENTER, 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM SETLEAF
WHERE SETCLASS = '0101' and SUBCLASS = :ControllingArea and SETNAME = :Group_Root_Node;
                                                
LT_HIERARCHY = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_ROOT
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_ROOT2;

while 1 = 1 do

LT_HIERARCHY_TMP = SELECT part.MANDT AS MANDT, part.SETNAME AS PARENT, part.SUBSETNAME AS GROUP_NODE, NULL AS COST_CENTER, :LEV + 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM :LT_HIERARCHY as main inner join SETNODE as part ON (part.MANDT = main.MANDT and main.GROUP_NODE = part.SETNAME)
WHERE part.SETCLASS = '0101' and part.SUBCLASS = :ControllingArea and main.LEVEL = :LEV;
     
SELECT COUNT(*) INTO NUM FROM :LT_HIERARCHY_TMP;
     
if :NUM = 0 then
       break;
       end if;
     
LT_HIERARCHY = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_HIERARCHY
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_HIERARCHY_TMP;
     
LEV := :LEV + 1;
end while;

LT_CC_TMP = SELECT CC.MANDT AS MANDT, CC.SETNAME AS PARENT, NULL AS GROUP_NODE, CC.VALOPTION AS VALOPTION, CC.VALFROM as VALFROM, CC.VALTO as VALTO, CC_gr.LEVEL + 1 AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM :LT_HIERARCHY as CC_gr inner join SETLEAF as CC ON (CC.MANDT = CC_gr.MANDT and CC_gr.GROUP_NODE = CC.SETNAME)                      
WHERE CC.SETCLASS = '0101' and CC.SUBCLASS = :ControllingArea;
                                                
LT_CC = SELECT MANDT, PARENT, GROUP_NODE, VALFROM AS COST_CENTER, LEVEL, CONTROLLING_AREA
FROM :LT_CC_TMP;

                                
LT_CC_TMP2 = SELECT CC.MANDT AS MANDT, CC.PARENT AS PARENT, CC.GROUP_NODE AS GROUP_NODE, CSKS.KOSTL AS COST_CENTER, CC.LEVEL AS LEVEL, :ControllingArea AS CONTROLLING_AREA
FROM :LT_CC_TMP as CC inner join CSKS as CSKS ON (CSKS.MANDT = CC.MANDT and CSKS.KOSTL >= CC.VALFROM and CSKS.KOSTL <= CC.VALTO)
WHERE CSKS.KOKRS = :ControllingArea;
                                                       
LT_CC = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_CC
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_CC_TMP2;

var_out = SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_HIERARCHY
UNION
SELECT MANDT, PARENT, GROUP_NODE, COST_CENTER, LEVEL, CONTROLLING_AREA from :LT_CC;

end if;                   

END /********* End Procedure Script ************/


After executing the view for the given hierarchy, and providing the following values for the two input parameters.
IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

The following output of the above scripted view, for the given cost center hierarchy:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

The above shown is the parent child relation of the hierarchy with cost center.


STEP 2: JOIN THE SCRIPTED HIERARCHY VIEW WITH THE TEXT TABLES TO GET DESCRIPTION TEXT

In order to get the description of a hierarchy, we need to join the scripted view (CA_HIERARCHY), with the two text tables i.e. SETHEADERT for group nodes and specific text tables for the module like CSKT for cost center, CEPCT for profit center text, etc. will be used. As in this case, we are implementing cost center hierarchy, so CSKT table will be used.
The following are the details of the calculation view:

NAMETYPEDATA CATEGORY
CA_HIERARCHY_DESCRIPTIONGraphicalDimension

Required Tables/Views:

NAMEVIEW/TABLECOMMENTS
CA_HIERARCHYVIEWScripted hierarchy view
SETHEADERTTABLETable containing description text for groups (i.e. COSTCENTERGROUP in this view)
CSKTTABLETable containing description text for cost center.

Input Parameters:

NAMEDATA TYPESIZECOMMENTS
ControllingAreaNVARCHAR12to input controlling area of the hierarchy (map with that of the scripted view, CA_HIERARCHY)
Group_Root_NodeNVARCHAR24to input the group (root) node (map with that of scripted view, CA_HIERARCHY)

Output Columns:

NAMEDATA TYPESIZECOMMENTS
MANDTNVARCHAR3Client
PARENTNVARCHAR24Parent group node
LEVELINTEGER-Level of the group node/cost center (for root node, LEV = 1)
CONTROLLING_AREANVARCHAR4Hierarchy controlling area (it will be same as the input controlling area)
LEVELNVARCHAR
24
Calculated column for level
Level_DescriptionNVARCHAR50Calculated Column for level text

Join details:

LEFT TABLELEFT FIELDSRIGHT TABLERIGHT FIELDSJOIN TYPEJOIN NAME
CA_HIERARCHYMANDT, GROUP_NODESETHEADERTClientTEXTJoin_1
JOIN_1MANDT, COST_CENTER, CONTROLLING_AREACSKTMANDT, KOSTL, KOKRSTEXTJoin_1

Filter details:

FILTER FIELDSTABLEFILTER CONDITIONCOMMENTS
SETCLASSSETHEADERTSETCLASS = 0101SETCLASS specific to cost center
SUBCLASSSETHEADERTSUBCLASS = 0001SUBCLASS specific to controlling area

As explained earlier, that the leaf node will either be a group node or cost center; or cost center will always be a leaf node. So, here these two columns will be combined into one, creating a calculated column, and doing the same for description.
Following are the two calculated columns:

NAMEDATA TYPE
LevelNVARCHAR
Level_DescriptionNVARCHAR


SIZEFORMULA
50if(isnull("GROUP_NODE"),"COST_CENTER","GROUP_NODE")
50if(isnull("DESCRIPT"),"LTEXT","DESCRIPT")

Shown below is how the semantics will look:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Notice the hidden columns (As they are not required)
Shown below is how the graphical view will look:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Shown below is how the joins will look like:
Join_1

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Join_2

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA


OUTPUT:
Provide the values to the input parameters:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

The final output of this view:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

STEP 3: FLATTEN THE ABOVE VIEW TO GET LEVEL WISE DATA

The above created view with the group and cost center texts, will be flattened in this step.
This is done in order to get level wise data, i.e. each row for each traversal from root node to a leaf node. And addition to that, a calculated column will be created, which will contain leaf node of each field. This calculated column can be used to join the hierarchy with the transaction data (which is one of the major reasons to flatten this hierarchy).

The following are the details of the calculation view:


NAMETYPEDATA CATEGORY
CA_HIERARCHY_DESCRIPTIONGraphicalDimension

Required Tables/Views:

NAMEVIEW/TABLECOMMENTS
CA_HIERARCHY_DESCRIPTIONVIEWScripted hierarchy view

Input Parameters:

NAMEDATA TYPESIZECOMMENTS
ControllingAreaNVARCHAR12to input controlling area of the hierarchy (map with that of CA_HIERARCHY_DESCRIPTION view)
Group_Root_NodeNVARCHAR24to input the group (root) node (map with that of CA_HIERARCHY_DESCRIPTION view)

Output Columns:

NAMEDATA TYPESIZECOMMENTS
MANDTNVARCHAR3Client
CONTROLLING_AREANVARCHAR4Hierarchy controlling area (it will be same as the input controlling area)
LEVEL1NVARCHAR24Flattened column for level1
LEVEL1_DESCRIPTIONNVARCHAR50Flattened column for level1 text
LEVEL2NVARCHAR24Flattened column for level2
LEVEL2_DESCRIPTIONNVARCHAR50Flattened column for level2 text
LEVEL3NVARCHAR24Flattened column for level3
LEVEL3_DESCRIPTIONNVARCHAR50Flattened column for level3 text
LEVEL4NVARCHAR24Flattened column for level4
LEVEL4_DESCRIPTION
NVARCHAR
50
Flattened column for level4 text
JOIN_FIELDNVARCHAR50Calculated column to create a join field (to join this hierarchy with transaction tables)

The maximum level here is 4, so the flattening here will be done till level 4. If the hierarchy goes beyond level 4, then further flattening will be required.

Join details:

LEFT TABLELEFT FIELDSRIGHT TABLE
CA_HIERARCHY_DESCRIPTION (LEVEL = 1)MANDT, LEVELCA_HIERARCHY_DESCRIPTION (LEVEL = 2)
CA_HIERARCHY_DESCRIPTION (LEVEL = 2)MANDT, LEVELCA_HIERARCHY_DESCRIPTION (LEVEL = 3)
CA_HIERARCHY_DESCRIPTION (LEVEL = 3)MANDT, LEVELCA_HIERARCHY_DESCRIPTION (LEVEL = 4)


RIGHT FIELDSJOIN TYPEJOIN NAME
MANDT, PARENTLeft OuterJoin_1
MANDT, PARENTLeft OuterJoin_2
MANDT, PARENTLeft OuterJoin_3

Filter details:

FILTER FIELDSTABLE/VIEWSFILTER CONDITIONCOMMENTS
LEVELCA_HIERARCHY_DESCRIPTIONLEVEL = 1This is to get level1 node and description
LEVELCA_HIERARCHY_DESCRIPTIONLEVEL = 2This is to get level2 node and description
LEVELCA_HIERARCHY_DESCRIPTIONLEVEL = 3This is to get level3 node and description
LEVELCA_HIERARCHY_DESCRIPTIONLEVEL = 4This is to get level4 node and description

Calculated columns:

NAMEDATA TYPE
JOIN_FIELDNVARCHAR


SIZEFORMULA
24if(isnull("LEVEL4"),if(isnull("LEVEL3"),if(isnull("LEVEL2"),"LEVEL1",
"LEVEL2"), "LEVEL3"),"LEVEL4")

Shown below is how the graphical view will look:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Shown below is how the joins will look like:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

This is the join between CA_HIERARCHY_DESCRIPTION (LEVEL=1) and CA_HIERARCHY_DESCRIPTION (LEVEL=2) i.e.Join_1.

So the following is how the semantics will look:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

OUTPUT:
Provide the values to the input parameters:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

The final output of this view:

IMPLEMENTING AND DISPLAYING STANDARD HIERARCHY WITH SAP HANA

Source: scn.sap.com

No comments:

Post a Comment