I have a table with columns and data as follows:
Table1
ID Name PID
A1 Apple P1
B1 Book A1
B2 Brook A1
C1 Cat B1
C2 Cook B1
C3 Car B1
D1 Dog B2
D2 Doll B2
E1 Egg C1
I want the results as follows:
ID Name Depth
B1 Apple\Book 2
C1 Apple\Book\Cat 3
E1 Apple\Book\Cat\Egg 4
C2 Apple\Book\Cook 3
C3 Apple\Book\Car 3
B2 Apple\Brook 2
D1 Apple\Brook\Dog 3
D2 Apple\Brook\Doll 3
The relationship is that a row is a child of another row if PID of that row is equal to ID of the parent row.
Apple is the base. So the first statement would be something like:
Select ID, Name, 2 from Table1 where PID=(select ID from Table1 where Name='Apple');
My current solution is creating a lot of views and save all those similar statements like that to views and union them together. But I don’t want that. I want to finish that within 1 select statement.A
Edit With out the Apple.