Skip to Content
Author's profile photo Ravindra Channe

Flattening the hierarchy using SQL code

Currently HANA doesn’t provide the recursive traversing of Hierarchy like other database which provide this feature using CONNECT BY clause or by using CTE (Common Table Expression). The information on the Hierarchical Query and CTE can be found here.

The recursive feature is very much required in multiple use cases like the Employee Manager relationship, defining custom authorization based on Hierarchy based information and so on.

The following code helps flattening the hierarchy and inserts the data into another table which can be subsequently used for the appropriate application.

The sample base table can contain data like:

Base_data.JPG

The hierarchy data can be traversed recursively and flattened to provide the output like:

final_output.JPG

The SQL Code with the sample data can be found below:

— Base table and data —

CREATE COLUMN TABLE T_HIER (NODE smallint, NODENAME varchar(2), PARENTNODE smallint);

insert into T_HIER values (1, ”, null);

insert into T_HIER values (2, ‘A’, 1);

insert into T_HIER values (3, ‘B’, 1);

insert into T_HIER values (4, ‘C’, 2);

insert into T_HIER values (5, ‘D’, 2);

insert into T_HIER values (6, ‘E’, 3);

insert into T_HIER values (7, ‘F’, 4);

— check the table contents —

select * from t_hier;

— Ignore the drop table error first time —

DROP TABLE TC_R_FLAT_HIER;

CREATE COLUMN TABLE TC_R_FLAT_HIER (NODE smallint, NODENAME varchar(2), PARENTNODE smallint, PARENTNODENAME varchar(2), CSTATUS varchar(1));

— Ignore the drop procedure error first time —

DROP PROCEDURE PR_R_FLAT_HIER_01;

CREATE PROCEDURE PR_R_FLAT_HIER_01 ()

LANGUAGE SQLSCRIPT AS

   vl_cnt       integer := 1;

BEGIN

— Empty the table for the new search

   delete from TC_R_FLAT_HIER;

— Insert data for the first level objects

   insert into TC_R_FLAT_HIER (NODE, NODENAME, PARENTNODE, PARENTNODENAME, CSTATUS)

   select C.NODE, C.NODENAME, C.PARENTNODE, P.NODENAME, ‘n’

   from T_HIER C left outer join T_HIER P on C.PARENTNODE = P.NODE;

— Check if there are any objects to search 

   select count(*) into vl_cnt from TC_R_FLAT_HIER where CSTATUS = ‘n’;

  

   WHILE :vl_cnt > 0 DO

—    Set the objects to Search from New

      update TC_R_FLAT_HIER set CSTATUS = ‘s’ where CSTATUS = ‘n’;

  

      insert into TC_R_FLAT_HIER (NODE, NODENAME, PARENTNODE, PARENTNODENAME, CSTATUS)

      select distinct B.PARENTNODE, B.NODENAME, C.PARENTNODE, P.NODENAME, ‘n’

      from TC_R_FLAT_HIER B left outer join T_HIER C on B.PARENTNODE = C.NODE

       left outer join T_HIER P on C.PARENTNODE = P.NODE

      where B.CSTATUS = ‘s’

      and B.PARENTNODE is not null;

—    Set the objects to Search Completed from Search

      update TC_R_FLAT_HIER set CSTATUS = ‘c’ where CSTATUS = ‘s’;

     

—    Check if there are any objects to search 

      select count(*) into vl_cnt from TC_R_FLAT_HIER where CSTATUS = ‘n’;

   END WHILE;

END;

— check the flattended hierarchy table contents —

select * from TC_R_FLAT_HIER order by 1;

Please feel free to comment / suggest improvements to the code. Hope this helps in some of your use cases.

Assigned Tags

      6 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Rama Shankar
      Rama Shankar

      Good stuff - thanks!

      Author's profile photo Jungwoo Han
      Jungwoo Han

      If you want to use hierarchy function in a SQL, you refer below site.

      https://wiki.wdf.sap.corp/wiki/display/ngdb/Hierarchies

      If you want some more guides and examples, free free to let me know.

      thx.

      jw.

      Author's profile photo Benedict Venmani Felix
      Benedict Venmani Felix

      Hi Jungwoo,

      The link is inaccessible. I think it is a SAP internal site.

      Benedict

      Author's profile photo Yuki Sakumoto
      Yuki Sakumoto

      From HANA2, Hierarchy function is released.

      https://help.sap.com/viewer/4fe29514fd584807ac9f2a04f6754767/2.0.00/en-US/2969da89b87f4abd85fd0b5f9f5bc395.html

       

       

      Author's profile photo Former Member
      Former Member

      Nice blogĀ 

      Author's profile photo Former Member
      Former Member

      Hi ravi,

      How can we find circular references in such hierarchy structure in hana 1