-3

I have only one table

enter image description here

from the above table I need to find out the grandparent of child_ID.

The output must be like below

enter image description here

Please let me know the code either in SQL or in Qlik Sense.

Thanks in advance.

Monika Bhajipale
  • 503
  • 1
  • 7
  • 9
  • 1
    Can you share what you have tried so far? – gabe3886 Apr 25 '17 at 08:45
  • 1
    It's not clear to me that child_id d has a grandparent of a? I guess this should only be the case for child_id c. Or am I'm missing something? – Kevin Apr 25 '17 at 08:46
  • 1
    how come child_id b's parent_id and grandparent_id is 'a'? How can they both be same? – India.Rocket Apr 25 '17 at 08:53
  • The expected output is not the grandparent id, but to list the ultimate parent (root) for each child. Also, there is no question in your post, only a code request. – Shadow Apr 25 '17 at 08:59
  • @shadow i think this is the question "I need to find out the grandparent of child_ID." which is there in the post – India.Rocket Apr 25 '17 at 09:02
  • Possible duplicate of [Getting root parent](http://stackoverflow.com/questions/8480095/getting-root-parent) – Shadow Apr 25 '17 at 09:03
  • 2
    @India.Rocket that's not a question. That's a statement of a requirement. – Shadow Apr 25 '17 at 09:04

1 Answers1

0

From you example it's hard to judge what you actually want to do, but I am assuming you are trying to create a Hierarchy.

In Qlik, what you are looking for is called Hierarchy

For your example, see below script:

Data:
LOAD * INLINE [
    Parent_ID, Child_ID
    a, b
    b, c
    c, d
    d, e
];

Hierarchy (Child_ID, Parent_ID, Title, 'Parent')
LOAD 
Parent_ID,
Child_ID,
Parent_ID as Title
Resident Data;

Since you do not have a title, I used Parent_ID as the title.

Shaun
  • 559
  • 1
  • 3
  • 17