【问题标题】:How to get child data from a self referencing table in sql如何从sql中的自引用表中获取子数据
【发布时间】:2017-02-01 13:19:43
【问题描述】:

我正在使用 sql 2012,并且我有一个具有 3 个级别的自引用表。表结构如下:

表结构

我有另一个表正在引用这个表,它有 ID 作为外键,所以如果该表的外键是 6,我需要显示 6 是“AAA”,它是“的子节点” AA”是“A”的子节点。我需要向下钻取到下层,从下层我应该能够上到上层。目前可以升到二级。

下面是引用另一个表的表的结构。

所以我想报告这两个表,最终输出应如下所示:

如果我的问题不太清楚,请询问我会尽力澄清它。

【问题讨论】:

  • 发布您当前的尝试和预期的输出。
  • 你为什么不使用hierarchyid?层数是固定的吗?如果是,则不需要递归或 CTE,只需 3 个自连接。代码会比 CTE 更简单,性能也更快
  • 不清楚CategoryName、CategoryType 和SubCategory。 Type 是什么?
  • 那是CategoryName子,如果Category Name是A则CategoryType是AA SubCategory就是AAA,不好意思命名不好。

标签: sql-server ssrs-2012


【解决方案1】:

假设类别树的深度不超过 3 级,这应该可行:

declare @Catergory table (
    ID int not null,
    Name nvarchar(10) not null,
    ParentID int null
)
declare @Customer table (
    ID int not null,
    Name nvarchar(10) not null,
    SurName nvarchar(10) not null,
    Address nvarchar(30) not null,
    CategoryId int not null
)
insert into @Catergory (ID, Name, ParentID)
values (1, 'A', null), (2, 'B', null),
    (3, 'C', null), (4, 'AA', 1),
    (5, 'CC', 3), (6, 'AAA', 4),
    (7, 'BB', 2), (8, 'AAA', 4),
    (9, 'CCC', 5), (10, 'AA', 1)

insert into @Customer (ID, Name, SurName, Address, CategoryId)
values (1, 'Duck', 'Duffy', '10 Steet', 10),
 (2, 'Ben', 'Ten', '10 Steet', 6),
 (3, 'Cat', 'Dog', '10 Steet', 5),
 (4, 'Chicken', 'Wings', '10 Steet', 1),
 (5, 'Fish', 'Water', '10 Steet', 7)

-- build structure using assumption that the depth is max three levels

select *
from @Customer cust
    join (
        select ID, Name as CategoryName, null As CategoryType, null as SubCategory from @Catergory roots where ParentID is null
        union
        select mids.ID, roots.Name, mids.Name, null from @Catergory mids
            join @Catergory roots on mids.ParentID = roots.ID and roots.ParentID is null
        union
        select leafs.ID, roots.Name, mids.Name, leafs.Name from @Catergory leafs
            join @Catergory mids on leafs.ParentID = mids.ID 
            join @Catergory roots on mids.ParentID = roots.ID and roots.ParentID is null
    ) as struct on cust.CategoryId = struct.ID
order by cust.id

输出:

+----+---------+---------+----------+------------+----+--------------+--------------+-------------+
| ID |  Name   | SurName | Address  | CategoryId | ID | CategoryName | CategoryType | SubCategory |
+----+---------+---------+----------+------------+----+--------------+--------------+-------------+
|  1 | Duck    | Duffy   | 10 Steet |         10 | 10 | A            | AA           | NULL        |
|  2 | Ben     | Ten     | 10 Steet |          6 |  6 | A            | AA           | AAA         |
|  3 | Cat     | Dog     | 10 Steet |          5 |  5 | C            | CC           | NULL        |
|  4 | Chicken | Wings   | 10 Steet |          1 |  1 | A            | NULL         | NULL        |
|  5 | Fish    | Water   | 10 Steet |          7 |  7 | B            | BB           | NULL        |
+----+---------+---------+----------+------------+----+--------------+--------------+-------------+

其中还有一些额外的列,但我相信您可以摆脱它们。请注意,尽管某些目录列具有 null 值。这是因为如果客户属于顶级或中级类别,则没有合理的方式来填写这些列。

【讨论】:

  • 非常感谢,我已经尝试存档好几天了。你是明星。它运行良好。
猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多