Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3344020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:01:17+00:00 2026-05-18T01:01:17+00:00

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

  • 0

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:01:18+00:00Added an answer on May 18, 2026 at 1:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that gets arranged into a hierarchy, such as this: class
I'm able to generate a hierarchy with a recursive CTE but I have always
i have a recursive query like this (note: this is just an example): var
I have a recursive model like this: public class Node { public int Id
I have this recursive method which deletes empty folders: private void DeleteEmpty(DirectoryInfo directory) {
i have this recursive code <ul> <% sql=SELECT * FROM cats ORDER BY orderid
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
I'm using SQLServer 2008. Say I have a recursive hierarchy table, SalesRegion, whit SalesRegionId
I have imported a huge hierarchy of maven projects into IntelliJ idea, now idea
I have a database table that represents a hierarchy of files and directories, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.