链接的行,单表SQL

时间:2012-09-23 08:38:32

标签: c# sql ms-access-2010

我有一张零件和不同阶段的桌子。 FromID=0是输入部件材料的地方。处理part1part2,然后汇总到part3

ID    Partname   FromID     Parts     Process
----------------------------------------------------
1     part1           0        10     process1 part1       
2     part1           1         0     process2 part1
3     part2           0        10     process1 part2
4     part2           1         0     process2 part2
5     part3           2         0     Assembly part1
6     part3           4         0     Assembly part2

有没有办法编写查询来获取可以处理嵌套子部分的零件及其所有子部件?我正在使用MS Access 2010和C#。

结果应如下所示

PartName = part1

ID    Partname   FromID     Parts     Work
-------------------------------------------
1     part1           0        10     work1 part1       
2     part1           1         0     work2 part1

PartName = part2

ID    Partname   FromID     Parts     Work
-------------------------------------------
3     part2           0        10     work1 part2
4     part2           1         0     work2 part2

PartName = part3

ID    Partname   FromID     Parts     Work
-------------------------------------------
1     part1           0        10     work1 part1       
2     part1           1         0     work2 part1
5     part3           2         0     Assembly part1
3     part2           0        10     work1 part2
4     part2           1         0     work2 part2
6     part3           4         0     Assembly part2

1 个答案:

答案 0 :(得分:0)

使用修改的预订树遍历来标记行以进行快速查询。看看这篇文章:

http://iamcam.wordpress.com/2006/03/24/storing-hierarchical-data-in-a-database-part-2a-modified-preorder-tree-traversal/

如果您计划在SQL服务器上部署,可以考虑使用公用表表达式:

http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx