使用 SSIS 将递归层次结构展平为维度

发布于 2024-09-30 03:46:19 字数 997 浏览 3 评论 0原文

我在关系数据库中有一个递归层次结构,这反映了团队及其在层次结构中的位置。

我希望将这个层次结构扁平化为数据仓库的维度,它是一个 SQL Server 数据库,使用 SSIS 到 SSAS。

我有一个表,teams:

teamid Teamname
1      Team 1
2      Team 2

和一个表 teamhierarchymapping:

Teamid  heirarchyid
1       4
2       2

和一个表层次结构:

sequenceid  parentsequenceid  Name
1           null              root
2           1                 Level 1.1
3           1                 Level 1.2
4           3                 Level 1.2 1

Giving

      Level 1.1 (Contains Team 2)
root <
      Level 1.2 <
                 Level 1.2 1 (Contains Team 1)

我想将其扁平化为一个维度,例如:

Team Name   Level 1    Level 2    Level 3
Team 1      Root       Level 1.1  [None]
Team 2      Root       Level 1.2  Level 1.2 1

我尝试了各种令人讨厌的 SQL 集来尝试将其组合在一起,以及 SSIS 中的各种管道(我刚刚开始学习),并且我没有找到将其整合在一起的解决方案。

有人可以帮忙吗?

(我认为使用示例数据编辑更正的问题)

I have a recursive hierarchy in a relational database, this reflects teams and their position within a hierarchy.

I wish to flatten this hierarchy into a dimension for data warehousing, it's a SQL Server database, using SSIS to SSAS.

I have a table, teams:

teamid Teamname
1      Team 1
2      Team 2

And a table teamhierarchymapping:

Teamid  heirarchyid
1       4
2       2

And a table hierarchy:

sequenceid  parentsequenceid  Name
1           null              root
2           1                 Level 1.1
3           1                 Level 1.2
4           3                 Level 1.2 1

Giving

      Level 1.1 (Contains Team 2)
root <
      Level 1.2 <
                 Level 1.2 1 (Contains Team 1)

I want to flatten this to a dimension like:

Team Name   Level 1    Level 2    Level 3
Team 1      Root       Level 1.1  [None]
Team 2      Root       Level 1.2  Level 1.2 1

I've tried various nasty sets of SQL to try and bring that together, and various piping around in SSIS (which I am just starting to pick up), and I'm not finding a solution that brings it together.

Can anyone help?

(Edit corrected issue with sample data, I think)

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

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

发布评论

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

评论(2

有木有妳兜一样 2024-10-07 03:46:19

您的样本数据有错误吗?我看不到层次结构映射如何连接到层次结构表来获得你想要的结果,除非层次结构映射是 teamid 1 => hierid 2 和 teamid 2 => hierid 4.

SSIS 可能无法(轻松)做到这一点,因此最好创建一个执行以下格式 SQL 的 OLEDB Source。请注意,这确实假设您使用的是 SQL Server 2008,因为其中引入了“PIVOT”功能...

WITH hier AS (
    SELECT parentseqid, sequenceid, hiername as parentname, hiername FROM TeamHierarchy
    UNION ALL
    SELECT hier.parentseqid, TH.sequenceid, hier.parentname, TH.hiername FROM hier
        INNER JOIN TeamHierarchy TH ON TH.parentseqid = hier.sequenceid
    ),
    teamhier AS (
        SELECT T.*, THM.hierarchyid FROM Teams T
            INNER JOIN TeamHierarchyMapping THM ON T.teamid = THM.teamid
    )
    SELECT *
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY teamname ORDER BY teamname, sequenceid, parentseqid) AS 'Depth', hier.parentname, teamhier.teamname
                FROM hier
                    INNER JOIN teamhier ON hier.sequenceid = teamhier.hierarchyid
        ) as t1
        PIVOT (MAX(parentname) FOR Depth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])) AS pvtTable
        ORDER BY teamname;

这有一些不同的元素,可能有更好的方法来做到这一点,但是对于扁平化层次结构,CTE 是理想的选择。

创建了两个 CTE:“hier”负责扁平化层次结构,“teamhier”只是一个辅助“视图”,使稍后的连接变得更简单。如果你只是采用层次 CTE 并运行它,你将得到你的扁平化视图:

WITH hier AS (
    SELECT parentseqid, sequenceid, hiername as parentname, hiername FROM TeamHierarchy
    UNION ALL
    SELECT hier.parentseqid, TH.sequenceid, hier.parentname, TH.hiername FROM hier
        INNER JOIN TeamHierarchy TH ON TH.parentseqid = hier.sequenceid
    )
    SELECT * FROM hier ORDER BY parentseqid, sequenceid

它的下一部分基本上采用这个扁平化视图,将其连接到你的团队表(以获取团队名称)并使用 SQL Server 的 PIVOT 进行旋转它是圆形的,让一切都按照你想要的方式对齐。有关 PIVOT 的更多信息可在 MSDN 上找到

如果您使用的是 SQL Server 2005,那么您可以只采用层次结构扁平化位,并且您应该能够使用 SSIS 的本机“PIVOT”转换块来完成肮脏的旋转工作。

Do you have an error in your sample data? I can't see how the hierarchy mapping connects to the hierarchy table to get the results you want, unless the hierarchy mapping is teamid 1 => hierid 2 and teamid 2 => hierid 4.

SSIS may not be able to do it (easily), so it may be better to create a OLEDB Source that executes SQL of the following format. Note this does assume you're using SQL Server 2008 as the 'PIVOT' function was introduced there...

WITH hier AS (
    SELECT parentseqid, sequenceid, hiername as parentname, hiername FROM TeamHierarchy
    UNION ALL
    SELECT hier.parentseqid, TH.sequenceid, hier.parentname, TH.hiername FROM hier
        INNER JOIN TeamHierarchy TH ON TH.parentseqid = hier.sequenceid
    ),
    teamhier AS (
        SELECT T.*, THM.hierarchyid FROM Teams T
            INNER JOIN TeamHierarchyMapping THM ON T.teamid = THM.teamid
    )
    SELECT *
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY teamname ORDER BY teamname, sequenceid, parentseqid) AS 'Depth', hier.parentname, teamhier.teamname
                FROM hier
                    INNER JOIN teamhier ON hier.sequenceid = teamhier.hierarchyid
        ) as t1
        PIVOT (MAX(parentname) FOR Depth IN ([1],[2],[3],[4],[5],[6],[7],[8],[9])) AS pvtTable
        ORDER BY teamname;

There's a few different elements to this, and there may be a better way to do it, but for flattening hierarchies, CTE's are ideal.

Two CTEs are created: 'hier' which takes care of flattening the hierarchy and 'teamhier' which is just a helper "view" to make the joins later on simpler. IF you just take the hier CTE and run it, you'll get your flattened view:

WITH hier AS (
    SELECT parentseqid, sequenceid, hiername as parentname, hiername FROM TeamHierarchy
    UNION ALL
    SELECT hier.parentseqid, TH.sequenceid, hier.parentname, TH.hiername FROM hier
        INNER JOIN TeamHierarchy TH ON TH.parentseqid = hier.sequenceid
    )
    SELECT * FROM hier ORDER BY parentseqid, sequenceid

The next part of it basically takes this flattened view, joins it to your team tables (to get the team name) and uses SQL Server's PIVOT to rotate it round and get everything aligned as you want it. More information on PIVOT is available on the MSDN.

If you're using SQL Server 2005, then you can just take the hierarchy flattening bit and you should be able to use SSIS's native 'PIVOT' transformation block to hopefully do the dirty pivoting work.

野生奥特曼 2024-10-07 03:46:19

您需要扁平化层次结构是否有原因?考虑 SSAS 中的父子层次结构维度类型,它可以处理可变深度层次结构,并允许使用扁平层次结构所不允许的额外功能/特性:

http://msdn.microsoft.com/en-us/library/ms174846.aspx

http://msdn.microsoft.com/en-us/library/ms174935.aspx

Is there a reason why you need to flatten the hierarchy? Consider the parent-child hierarchy dimension type in SSAS, this handles variable-depth hierarchies, and would allow extra functions/features that a flattened hierarchy would not:

http://msdn.microsoft.com/en-us/library/ms174846.aspx

http://msdn.microsoft.com/en-us/library/ms174935.aspx

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