多父级递归

发布于 2024-12-13 09:21:32 字数 595 浏览 3 评论 0原文

Master Table
===========
ID NAME
1   A
2   B
3   C
4   D
5   E

具有多个父级的层次结构表(请注意,由于重复值,两者都不能成为主列):

Relations Table
================
ChildID ParentID  
   3        1
   3        2
   4        3
   4        2
   5        4

层次结构变得像(它可能并不总是这样线性):

  1         2
  |         |
  3         3
  |         |
  4         4
  |         |
  5         5

出于报告目的,我需要递归层次结构格式的数据,以便我可以深入了解它。我不知道是否可以从现有数据本身获取向下钻取功能(似乎不可行,因为由于重复值,我无法创建递归父子关系)。

你有什么想法吗?我的目标是最终使用这个结构作为 SSAS 中的一个维度,如果表具有自主键-子键关系,它会自动提供向下钻取。

Master Table
===========
ID NAME
1   A
2   B
3   C
4   D
5   E

Hierarchy table with multiple parents (Note that neither can be primary column due to duplicate values):

Relations Table
================
ChildID ParentID  
   3        1
   3        2
   4        3
   4        2
   5        4

Hierarchy becomes like (it might not be this linear always):

  1         2
  |         |
  3         3
  |         |
  4         4
  |         |
  5         5

For reporting purpose I need data in recursive hierarchy format so that I can drill down it. I'm not getting if I can get drill down feature from existing data itself (seems not doable as I can not create recursive parent-child relationship due to duplicate values).

Do you have any ideas? My goal is to finally use this structure as a dimension in SSAS which automatically gives drill down if a table has a self primary key-child key relationship.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2024-12-20 09:21:32

使用您的示例数据,我实际上得到了一棵不同的树...

Relations Table            Tree
================          =======
ChildID ParentID           1   2
   3        1               \ /|
   3        2                3 |
   4        3                 \|
   4        2                  4
   5        4                  |
                               5

您实际上想要两棵独立的树吗?如果是这种情况,您可以引入一个额外的字段,例如树 id...

Relations Table              Tree1     Tree2
=======================      =====     =====
TreeID ParentID ChildID        
   1     NULL      1           1         2
   1      1        3           |         |
   1      3        4           3         3
   1      4        5           |         |
   2     NULL      2           4         4
   2      2        3           |         |
   2      3        4           5         5
   2      4        5

如果没有一些额外的信息,您将始终遇到分支分裂和合并的问题,而没有一组非常好的约束。例如,如果您想要 1-3-4-5 和 2-3-4-6 的两个线性树,您当前的模型将具有此...

Relations Table            Tree
================          =======
ParentID ChildID           1   2
   1        3               \ /
   2        3                3 
   3        4                |
   4        5                4
   4        6               / \
                           5   6

但您现在遇到的问题是有四个线性路径。 ..
- 1-3-4-5
- 1-3-4-6
- 2-3-4-5
- 2-3-4-6

您可能需要描述一个现实世界的情况,准确地描述您想要从中得到什么,以及准确地描述您不想要从中得到什么。

我的典型经验是,出于报告目的,树中的任何节点都应该只有一个父节点,但可能有许多子节点。这意味着当爬上一棵树时,你只有一条路线,而当爬下一棵树时,数据会分成子节点。

有许多父母和许多孩子会形成一张网,而不是一棵树。无论您从哪个方向遍历树,都有多条路线。

Using your example data, I actually get a different tree...

Relations Table            Tree
================          =======
ChildID ParentID           1   2
   3        1               \ /|
   3        2                3 |
   4        3                 \|
   4        2                  4
   5        4                  |
                               5

Do you actually want two independent trees? If that's the case, you could introduce an extra field such as a tree id...

Relations Table              Tree1     Tree2
=======================      =====     =====
TreeID ParentID ChildID        
   1     NULL      1           1         2
   1      1        3           |         |
   1      3        4           3         3
   1      4        5           |         |
   2     NULL      2           4         4
   2      2        3           |         |
   2      3        4           5         5
   2      4        5

Without some extra piece of information, you'll always have problems of branches splitting and merging without a very well formed set of constraints. For example, if you wanted two linear trees of 1-3-4-5 and 2-3-4-6, your current model would have this...

Relations Table            Tree
================          =======
ParentID ChildID           1   2
   1        3               \ /
   2        3                3 
   3        4                |
   4        5                4
   4        6               / \
                           5   6

The problem you now have though, is that there are FOUR linear paths...
- 1-3-4-5
- 1-3-4-6
- 2-3-4-5
- 2-3-4-6

What may be required is for you to describe a real world situation, exactly what you want from it, and exactly what you don't want from it.

My typical experience is that, for reporting purposes, any node in a tree should only have one parent, but may have many children. This means that when climbing up a tree you only have one route, and when climbing down a tree the data separates into sub-nodes.

Having many parents and many children makes a web rather than a tree. Where you have multiple routes, no matter which direction you traverse the tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文