sql - Query to List all hierarchical parents and siblings and their childrens, but not list own childrens -


I have a basic SQL table, which has a simple successor connection between each row. This is a ParentID for each line and it is used by the second row, as follows:

  Account ID | Accountant | Parent id --------------------------------------- 1 Matthew 2 Philippe 3 John 2 4 Susan 2 5 Anita 1 6 Amy 1 7 Elsa 3 8 Anna 7 ............................. ........ ......................... 45 Christoph 8   

Hope the structure is clear

But the need to make this list is a bit weird when we pass an account ID, then all its parents and siblings and siblings should be listed. But it never lists any child of that account at any level. I can explain that in a little more detail with a picture. Sorry for the clarity of the picture .. I have an old phone camera. Enter image details here

When we pass Account ID 4 , then all parents and their siblings should be listed, but it's 4,6,7,8,9 , 10 should not be listed. This means that the account and any of its children should be protected from the consequences (depending on the elements of the picture trees) hope that the explanation is clear.

If I found it right and you have to leave the whole table, 4 and all of it Then try this recurring query on the offspring:

 with  as the whistle (SELECT * FROM T WHERE Account ID = 4 Unius Select All T. T Jane CT On T. Parent ID = CT Accident) Select * From T to where there is no Account ID (Select from Seat Account)   

In the comment question's answer :

Then it will not go to the top, it comes only for the specified account, for example, if I pass 4 as the first parameter and 2 as the second parameter, Then the results should be 2,5,11,12

You should start with ID = 2 and visit the id = 4 below so that you can go after id = 4 Whole sub-cut cut:

  with whistle (SELECT * FROM T WHERE Account ID = 2 Union All Receive select t. * Add T whistle on T .ParentID = CT.AccountId WHERE T.AccountId & lt;> 4) SELECT * FROM CT    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -