I have a table with 3 columns:
Id (identity),
Sku_Parent (varchar(10)),
Sku_Child (varchar(10))
I would like to get help to write a recursive query to obtain up to 5 parent-child relationship levels.
When a newly derived product gets inserted, the new derived product also (eventually) can be used as a parent. I want to be able to track the history of each new derived product, 5 levels down and find the top parent. I hope this is a clear explanation.
Here is a sample data that currently resides in my table:
7038 N0179890 N0180323
7039 N0180323 N0180328
7040 N0180323 N0180329
7041 N0180323 N0180330
7042 N0180323 N0180331
7043 N0180323 N0180332
7044 N0180323 N0180333
7045 N0180323 N0180334
I have found examples, but those I found assume that there is only one parent record and has a null value.
This is a link to get a copy of table data:
https://drive.google.com/open?id=1hP7kRQsl_8YzEu4dK9Z8J91y-2Gvh7Qi
Please advice. Many thanks.
UPDATE
I see that sku P55645 was used to create 9 derived products, and in this case none of the derived products became a parent. That is totally ok.
I just think that it might be easier to read if rather than display 9 rows, each one of the skus gets stuffed in a single line, starting with the top level sku.
As it currently is:
P0055645 P0098245 P0055645, P0098245
P0055645 P0110959 P0055645, P0110959
P0055645 P0110960 P0055645, P0110960
P0055645 P0110961 P0055645, P0110961
P0055645 P0110962 P0055645, P0110962
P0055645 P0110963 P0055645, P0110963
P0055645 P0110964 P0055645, P0110964
P0055645 P0110965 P0055645, P0110965
P0055645 P0157714 P0055645, P0157714
I think this might read better:
P0055645, P0098245, P0110959, P0110960, P0110961, P0110962, P0110963, P0110964, P0110965, P0157714
This is also current output that I am having a hard time following:
Parent Derived Hierarchy
P0172879 P0178192 P0172879, P0178192
P0178192 P0178206 P0172879, P0178192, P0178206 - until this point i see that the relationship of parent child and then the chil becoming a parent is well illustrated
P0178206 P0178219 P0172879, P0178192, P0178206, P0178219 -- but then P0178206 becomes the parent of 6 new skus. Do you think this looks good, or should the output be like the first example I described?
P0178206 P0178220 P0172879, P0178192, P0178206, P0178220
P0178206 P0178221 P0172879, P0178192, P0178206, P0178221
P0178206 P0178222 P0172879, P0178192, P0178206, P0178222
P0178206 P0178223 P0172879, P0178192, P0178206, P0178223
P0178206 P0178224 P0172879, P0178192, P0178206, P0178224
SECOND UPDATE
P0170926 P0170928 P0170926, P0170928 -- if sku load on form is P0170926, then the query will load a record-set of 2 records
P0170928 P0170929 P0170926, P0170928, P0170929 -- if user loads on form P0170928 then it will load 3 records including P0170926
P0170932 P0174069 P0170932, P0174069 -- here if user loads P0170932 the record set will only contain one record, shouldn't all the records up to P0174075 be also included?
P0170932 P0174070 P0170932, P0174070
P0170932 P0174071 P0170932, P0174071
P0170932 P0174072 P0170932, P0174072
P0170932 P0174073 P0170932, P0174073
P0170932 P0174074 P0170932, P0174074
P0170932 P0174075 P0170932, P0174075