Minimize
15

I will not go into the details of how the bridge table works. You may find that information on Kimballs web site and in some of his books. The first script creates a CTE which allows you to test some simple examples using the SQL 2005\2008 AdventureworksDW database.

 

 


WITH Tree(Parent_ID, Child_ID, Parent_Level, Child_Level, PathFromRoot, PathFromParent, Top_Flag, Bottom_Flag) AS
(--Get the root member
  SELECT ParentEmployeeKey, EmployeeKey as Child_ID, 0 AS Parent_Level, 0 AS Child_Level
        ,Convert(varchar(max),EmployeeKey) as PathFromRoot, Convert(varchar(max),EmployeeKey) as PathFromParent
        ,Case WHEN ParentEmployeeKey is Null Then 'Y' Else 'N' End as Top_Flag, 'N' as Bottom_Flag
    FROM dbo.DimEmployee
    WHERE ParentEmployeeKey IS NULL  
UNION ALL
    SELECT e.ParentEmployeeKey, e.EmployeeKey, Child_Level, Child_Level + 1, PathFromRoot + ' ' + Convert(varchar(max),EmployeeKey)
       , Convert(varchar(max),e.ParentEmployeeKey) + ' ' + Convert(varchar(max),e.EmployeeKey) as PathFromRoot,'N', CASE WHEN EXISTS(SELECT 1 from dbo.DimEmployee e2 where e.EmployeeKey = e2.ParentEmployeeKey) THEN 'N' Else 'Y' END
    FROM dbo.DimEmployee e
        INNER JOIN Tree t
        ON e.ParentEmployeeKey = t.Child_ID
    )
, Navigation_Bridge(Parent_ID, Child_ID, Parent_Level, Child_Level, Distance_Between_Parent_And_Child, PathFromRoot, PathFromParent, Top_Flag, Bottom_Flag, Source) AS
   (SELECT ISNULL(Parent_ID, Child_ID), Child_ID, Parent_Level, Child_Level, Child_Level-Parent_Level, PathFromRoot,   PathFromParent, Top_Flag, Bottom_Flag, 0 as Source from Tree
    UNION --Create Self Referencing Rows. These should not be used for recursion in the following step.
   SELECT Child_ID, Child_ID, Child_Level, Child_Level, 0, PathFromRoot,Convert(varchar(max),Child_ID), Top_Flag, Bottom_Flag, -1 from Tree
      WHERE Child_Level > 0
   UNION ALL
   SELECT A.Parent_ID, B.Child_ID, A.Parent_Level,B.Child_Level, B.Child_Level-A.Parent_Level, B.PathFromRoot
       , A.PathFromParent + ' ' + Convert(varchar(max),B.Child_ID)
       , A.Top_Flag,B.Bottom_Flag, Source + 1
      FROM Navigation_Bridge A INNER JOIN Tree B on B.Parent_ID = A.Child_ID AND B.Child_Level >= 2 And Source != -1
)


--Select * From Navigation_Bridge
--WHERE Parent_ID = 3
/*
--Get the Children of Roberto Taburello (3)
SELECT
  FirstName + ' ' + LastName as Name
 ,*  FROM Navigation_Bridge nb
INNER JOIN dbo.DimEmployee e on nb.Child_ID = e.EmployeeKey
WHERE Parent_ID = 3 and Bottom_Flag = 'Y';
--SELECT * FROM DimEmployee
*/
/*
--Get all of the Parents of Roberto Tamburello (3)
SELECT
  FirstName + ' ' + LastName as Name
 , * FROM Navigation_Bridge nb
INNER JOIN dbo.DimEmployee e on nb.Parent_ID = e.EmployeeKey
WHERE Child_ID = 3;
*/

/*
--Get the Direct Reports of Roberto Taburello (3)
SELECT
  FirstName + ' ' + LastName as Name
 , * FROM Navigation_Bridge nb
INNER JOIN dbo.DimEmployee e on nb.Child_ID = e.EmployeeKey
WHERE Parent_ID = 3
 And Distance_Between_Parent_And_Child = 1;
*/

This next script allows you to create a small table for testing, and has some quick directions to allow you to customize this to your table.

/*
DROP TABLE dbo.Emp
GO
Create Table Emp
(Source_Child_Id int NOT NULL,
 Source_Parent_ID int Null
)
Delete from Emp
GO
INSERT INTO Emp Values(1,NULL)
INSERT INTO Emp Values(2,1)
INSERT INTO Emp Values(3,1)
INSERT INTO Emp Values(4,3)
INSERT INTO Emp Values(5,3)
INSERT INTO Emp Values(6,4)
INSERT INTO Emp Values(7,6)

GO

*/
--This sample works on the sample table above
--Tree is a straight Parent child tree. For each node there is a row from it to its direct parent.
--This also computes the Path from the Root to the child, and the child's level of depth from the root
--   as well as from its parent.
--Navigation Bridge is Kimball's navigation bridge with a row for every pair of nodes which are related to one another,
--  either directly or indirectly, as well as a row for each node pointing to itself.
--To use this with another table you simply need a table with a parent/child relationship.
--  Replace Source_Parent_ID with your parentkey
--  Replace Source_Child_ID with your childkey
--  Replace dbo.emp with your table name
WITH Tree(Parent_ID, Child_ID, Parent_Level, Child_Level, PathFromRoot, PathFromParent, Top_Flag, Bottom_Flag) AS
(--Get the root member
  SELECT Source_Parent_ID as Parent_ID, Source_Child_Id as Child_ID, 0 AS Parent_Level, 0 AS Child_Level
        ,Convert(varchar(max),Source_Child_Id) as PathFromRoot, Convert(varchar(max),Source_Child_Id) as PathFromParent
        ,Case WHEN Source_Parent_ID is Null Then 'Y' Else 'N' End as Top_Flag, 'N' as Bottom_Flag
    FROM dbo.Emp
    WHERE Source_Parent_ID IS NULL  
UNION ALL
    SELECT e.Source_Parent_ID, e.Source_Child_Id, Child_Level, Child_Level + 1, PathFromRoot + ' ' + Convert(varchar(max),Source_Child_Id)
       , Convert(varchar(max),e.Source_Parent_ID) + ' ' + Convert(varchar(max),e.Source_Child_Id) as PathFromRoot,'N'
       , CASE WHEN EXISTS(SELECT 1 from dbo.Emp e2 where e.Source_Child_Id = e2.Source_Parent_ID) THEN 'N' Else 'Y' END
    FROM dbo.Emp e
        INNER JOIN Tree t
        ON e.Source_Parent_ID = t.Child_ID
    )
, Navigation_Bridge(Parent_ID, Child_ID, Parent_Level, Child_Level, Distance_Between_Parent_And_Child, PathFromRoot, PathFromParent, Top_Flag, Bottom_Flag, Source) AS
   (SELECT ISNULL(Parent_ID, Child_ID), Child_ID, Parent_Level, Child_Level, Child_Level-Parent_Level, PathFromRoot,   PathFromParent, Top_Flag, Bottom_Flag, 0 as Source from Tree
    UNION --Create Self Referencing Rows. These should not be used for recursion in the following step.
   SELECT Child_ID, Child_ID, Child_Level, Child_Level, 0, PathFromRoot ,Convert(varchar(max),Child_ID), Top_Flag, Bottom_Flag, -1 from Tree
      WHERE Child_Level > 0
   UNION ALL
   SELECT A.Parent_ID, B.Child_ID, A.Parent_Level,B.Child_Level, B.Child_Level-A.Parent_Level, B.PathFromRoot
       , A.PathFromParent + ' ' + Convert(varchar(max),B.Child_ID)
       , A.Top_Flag,B.Bottom_Flag, Source + 1
      FROM Navigation_Bridge A INNER JOIN Tree B on B.Parent_ID = A.Child_ID AND B.Child_Level >= 2 And Source != -1
)
SELECT * FROM Navigation_Bridge
--Where Parent_ID = 4
;

 

Comments

There are currently no comments, be the first to post one.

Post Comment

Only registered users may post comments.

LatestArticles Minimize
Changing Granularity of Leaf Level Calculations in SSAS Tabular by Jason Thomas

Finding Nearest Stores using SSRS Map Reports by Jason Thomas

Heat Maps for SSRS using Map Control by Jason Thomas

Parameters for Analysis Services Reporting: Introduction, Pt. 3 by William Pearson
BI Architect Bill Pearson continues an extended examination of parameterization within Analysis Services reports. In this, Part 3 of the article, we continue to get hands-on practice cr...

Parameters for Analysis Services Reporting: Introduction, Pt. 2 by William Pearson
BI Architect Bill Pearson continues an extended examination of parameterization within Analysis Services reports. In this, Part 2 of the article, we continue to get hands-on practice cr...

Using Annotation Tables in SSAS to Show Last Processed Time and Latest Data Updates as a Measure in a Cube by John Hall

Using Color in SSRS Charts by Melissa Coates
Effective data presentation techniques help users  interpret information quickly and reliably.  Layout, formatting, sizing, labeling, and other report elements may all be used to facilitate ...

Is TOAD faster than BIDS Query Builder? by John Hall

SSRS: Unexplained Warning “This field is missing from the returned result set from the data source” And Checking a Field for Null Exception by John Hall
If you ever see the warning message “This field is missing from the returned result set form the data source” and get unexplained #Errors in columns on your report it may be from SSAS not returning a ...

Beyond Excel pivot tables: Leveraging cube formulas with MDX by Javier Guillen
PowerPivot and DAX are a powerful technologies, but there is still a good amount of work that can be done with traditional cube functions. In its current version, PowerPivot lacks the concept of ...


Advertisement Minimize

Advertisement Minimize

Copyright 2004-2012 MSBICentral.com Terms Of Use Privacy Statement