SQL - Return all child parts associated with parent part

31 Views Asked by At

I have a list of parent parts with child parts associated with them. However the child parts, can too be parent parts that have even more child parts associated.

The number of levels of parent child relation ships is is unknown.

How could I return all the child parts associated with a specific parent part.

In the data below I would want all the bottom level child parts from the parent part A.

Parent  Child
A       123
A       231
A       564
A       1089
123     SE123
123     DE123
231     SE231
231     DE231
564     L9
564     L10
564     L11
564     L12
564     L13
564     L14
DE231   H8
L14     L14.1
L14     L14.2

Would return:

1089
SE123
DE123
SE231
L9
L10
L11
L12
L13
H8
L14.1
L14.2

I am trying to do this using the SQL editor in Alteryx

1

There are 1 best solutions below

0
Matt On BEST ANSWER
WITH RECURSIVE ChildParts AS (
  SELECT Child
  FROM YourTable
  WHERE Parent = 'A'

  UNION ALL

  SELECT t.Child
  FROM YourTable t
  INNER JOIN ChildParts cp ON cp.Child = t.Parent
)
SELECT Child
FROM ChildParts;