将父/子表转换为固定列维表

发布于 2024-09-29 08:01:47 字数 945 浏览 4 评论 0原文

我有一个关系表(id、parentId、name)

,我想将其转换为扁平维度表

(id、Level1、Level2、Level3、Level4),

我可以将深度固定为 4 深。

我在递归 CTE 和枢轴方面取得了进展,但

我得到的

Id  Name   Level1 Level2
0   Root   NULL   NULL
1   NULL   L1     NULL

结果集不正确,但我需要

Id  Name   Level1 Level2
0   Root   NULL   NULL
1   Root   L1     NULL

这是我必须确定的日期,

with rcte as
(
      select h.id
      ,h.parent_id
      ,h.name
      ,1 as HierarchyLevel 
  FROM RelTable h
  where id = 1
  union all
  select h2.id
       , h2.parent_id 
      , h2.name
      , r.HierarchyLevel + 1 AS HierarchyLevel 
  FROM RelTable h2
  inner join rcte r on h2.parent_id = r.id
 )
select id, parent_id, [1] as L1,[2] as L2,[3] as L3, [4] as L4
from (
select id,parent_id,name,HierarchyLevel from rcte
) as src
pivot  ( max(name)  for HierarchyLevel   in ([1],[2],[3],[4]) ) as pvt

我做错了什么?

I've a relational table (id, parentId, name)

which I'd like to convert to a flattened dimentional table

(id, Level1, Level2, Level3, Level4)

I'm ok fixing the depth at 4 deep.

I've made progress with a recursive CTE and pivot, but the result set isn't right

I get

Id  Name   Level1 Level2
0   Root   NULL   NULL
1   NULL   L1     NULL

but I need

Id  Name   Level1 Level2
0   Root   NULL   NULL
1   Root   L1     NULL

here's what I have to date

with rcte as
(
      select h.id
      ,h.parent_id
      ,h.name
      ,1 as HierarchyLevel 
  FROM RelTable h
  where id = 1
  union all
  select h2.id
       , h2.parent_id 
      , h2.name
      , r.HierarchyLevel + 1 AS HierarchyLevel 
  FROM RelTable h2
  inner join rcte r on h2.parent_id = r.id
 )
select id, parent_id, [1] as L1,[2] as L2,[3] as L3, [4] as L4
from (
select id,parent_id,name,HierarchyLevel from rcte
) as src
pivot  ( max(name)  for HierarchyLevel   in ([1],[2],[3],[4]) ) as pvt

what am I doing wrong?

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

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

发布评论

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

评论(1

眼藏柔 2024-10-06 08:01:47

解决方案过于复杂?如果它固定在四个深度,那么可以通过一些简单的连接来完成...

SELECT
    L1.id as ID
    L1.Name as Level1
    L2.Name as Level2
    L3.Name as Level3
    L4.Name as Level4
FROM
    RelTable as L1

        INNER JOIN
    RelTable as L2
        ON L1.id = L2.ParentID

        INNER JOIN
    RelTable as L3
        ON L2.id = L3.ParentID

        INNER JOIN
    RelTable as L4
        ON L3.id = L4.ParentID

作为使用 CTE 的练习,它毫无用处,但它可以满足您的需要。

Overcomplicating the solution? If it's fixed at four deep then it can be done with some simple joins...

SELECT
    L1.id as ID
    L1.Name as Level1
    L2.Name as Level2
    L3.Name as Level3
    L4.Name as Level4
FROM
    RelTable as L1

        INNER JOIN
    RelTable as L2
        ON L1.id = L2.ParentID

        INNER JOIN
    RelTable as L3
        ON L2.id = L3.ParentID

        INNER JOIN
    RelTable as L4
        ON L3.id = L4.ParentID

As an exercise in using CTEs its useless, but it does what you need.

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