【发布时间】:2017-12-29 22:34:14
【问题描述】:
我有一个包含 3 列的表格:
Id (identity),
Sku_Parent (varchar(10)),
Sku_Child (varchar(10))
我想获得帮助以编写递归查询以获得最多 5 个父子关系级别。
当插入新的派生产品时,新派生产品也(最终)可以用作父产品。我希望能够跟踪每个新衍生产品的历史,向下 5 个级别并找到最高父级。我希望这是一个明确的解释。
这是当前位于我的表中的示例数据:
7038 N0179890 N0180323
7039 N0180323 N0180328
7040 N0180323 N0180329
7041 N0180323 N0180330
7042 N0180323 N0180331
7043 N0180323 N0180332
7044 N0180323 N0180333
7045 N0180323 N0180334
我找到了示例,但我找到的示例假设只有一个父记录并且具有空值。
这是获取表格数据副本的链接:
https://drive.google.com/open?id=1hP7kRQsl_8YzEu4dK9Z8J91y-2Gvh7Qi
请指教。非常感谢。
更新
我看到 sku P55645 用于创建 9 个派生产品,在这种情况下,没有派生产品成为父产品。没关系。
我只是认为,如果不是显示 9 行,而是将每个 sku 从顶级 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
第二次更新
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
【问题讨论】:
-
您希望如何读取数据?你能分享一个示例输出吗?
-
嗨,克里斯。对不起,我不得不开车回家。我还不清楚如何格式化输出。我想我只想能够指出关系树/分解我想说的是,非技术用户可以很容易地分辨出什么是顶级父 sku 以及孩子成为孩子和父母的顺序是什么。 /跨度>
-
顺便说一句,非常感谢您的帮助。
-
嗨,克里斯,我考虑了输出并查看了数据,我有一些想法想与您分享,看看您是否可以帮助我处理格式。我将更新我的问题并在其中包含示例输出。谢谢。
-
当然,去吧。 :)
标签: sql sql-server recursion hierarchical-data