How to derive the hierarchy output using SAP HANA SQL

71 views Asked by At

I came across some below hierarchical requirement. I have two columns called COL1 and COL2 and want to derive the value (MAP).

COL1 and COL2 have 4 types of relationships i.e. 1:1, 1:M, M:1, M:M.

Explanation - For example

  1. XX1 is mapped to A1 (temp result XX1 : A1)

  2. A1 is mapped to XX3 (temp result XX1, XX3 : A1)

  3. A1 is mapped to XX3 (temp result XX1, XX3 : A1)

  4. XX3 is mapped to A8 (temp result XX1, XX3 : A1, A8)

  5. A8 is mapped to XX9 (temp result XX1, XX3, XX9 : A1, A8)

Note - Traverse level not fixed, it can be any levels.

SQL solution is more preferred and Python solution is least preferred (in case of Python, I want to read the data from the file as I can't connect to DB through Python).

Is there anyway to achieve?

COL1    COL2    Map
XX1     A1      XX1,XX3,XX9 : A1,A8
XX2     A2      XX2,XX4 : A2
XX3     A1      XX1,XX3,XX9 : A1,A8
XX4     A2      XX2,XX4 : A2
XX5     A3      XX5:A3
XX6     A4      XX5:A4,A5
XX6     A5      XX5:A4,A5
XX7     A6      XX7,XX8:A6:A7
XX7     A7      XX7,XX8:A6:A7
XX8     A6      XX7,XX8:A6:A7
XX8     A7      XX7,XX8:A6:A7
XX3     A8      XX1,XX3,XX9 : A1,A8
XX9     A8      XX1,XX3,XX9 : A1,A8

I have attached the sample data as an image as well.

Sample Data

0

There are 0 answers