Kimball Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Populating the Bridge table (Tsql)

4 posters

Go down

Populating the Bridge table (Tsql) Empty Populating the Bridge table (Tsql)

Post  Toffeeman Fri Mar 04, 2011 10:46 am

I am trying to create a bridge table between my fact table and my company table

OrderFact
CustomerKey (FK)
DateKey (FK)
CompanyKey(FK)
ItemKey (FK)
ItemQty

CompanyBridge
ParentCompanyKey (FK)
SubsidiaryCompanyKey (FK)
DepthFromParent
LowestFlag
TopmostFlag

CompanyDimension
CompanyID (PK)
CompanyCode
CompanyName

This is trying to mimic the strcture outlined in the Kimball Group Reader P 357.

The bridge table should contain one record for each seperate path from each node in the company tree to itself and every node below it.

I am struggling with the Tsql statement to generate this - especially the DepthFromParent field.

Any help would be greatly appreciated.

Toffeeman

Posts : 2
Join date : 2011-03-04
Location : Cheshire, UK

Back to top Go down

Populating the Bridge table (Tsql) Empty Re: Populating the Bridge table (Tsql)

Post  John Simon Mon Mar 14, 2011 3:58 am

Check out my blog:

http://jsimonbi.wordpress.com/2011/01/15/kimball-hierarchy-bridge-table/


John Simon

Posts : 186
Join date : 2009-05-29
Location : Melbourne, Australia

http://jsimonbi.wordpress.com

Back to top Go down

Populating the Bridge table (Tsql) Empty Re: Populating the Bridge table (Tsql)

Post  ngalemmo Mon Mar 14, 2011 10:10 am

Here is another way to do it, using a loop. This particular code was written for Netezza, but should be easy enough to translate to other dialects.

The loop appends successive generations until there are no more. The code calculates level and distance from parent. You can always add other things if you need it.



CREATE OR REPLACE PROCEDURE NG_EXPLODE_TREE()
RETURNS INTEGER
EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC
declare dist integer;
todo integer;
BEGIN
/*
This sample procedure demonstrates how to create an exploded hierarchy brige from a recursive
tree structure without using recursion.

This example using three tables.

NGBASE contains the source recursive tree
NGTREE is the work table used by the process
NGEXPLODE is the resulting exploded bridge


Table structures are as follows:
NGBASE represents any source table. It would contain a child and parent key.
create table ngtree (child integer, parent integer, distance integer);
create table ngexplode (child integer, parent integer, level integer, distance integer);

Data types for child and parent are purely for this example. In practice they would reflect the data
types in the source.

*/

delete from ngtree; -- clean things out
delete from ngexplode;

/*
Load the working table with the parent child releationships from the source table. Initialize level to 1.
*/

insert into ngtree
select child, parent, 1 from ngbase;

/*
Starting with level 1 rows, insert new rows based on the parent's parent. Load as new level.
Increment level count and repeat until no more rows are inserted.
*/

dist := 1;
loop
INSERT INTO NGTREE
SELECT
A.CHILD,
B.PARENT,
dist+1
FROM NGTREE A, NGTREE B
WHERE A.DISTANCE = dist
AND A.PARENT = B.CHILD
AND B.DISTANCE = 1; -- get immediate parent

/*
Save the row_count value immediately after the statement.
Executing anything, including an assignment statement will change it.
*/

todo := row_count;
dist := dist + 1;

exit when todo = 0;
end loop;

/*
Created the exploded bridge table. Calculate child level and distance from parent.

Code assumes a root node is coded with a parent key = -1. Adjust accordingly.
*/

insert into ngexplode
SELECT
A.CHILD,
case when A.PARENT = -1 then a.child else a.parent end as parent,
B.DISTANCE AS CHILDLEVEL,
case when a.parent = -1 then 0 else A.DISTANCE end AS DISTANCE
FROM NGTREE A,
(SELECT CHILD, LEVEL FROM NGTREE WHERE PARENT = -1 ) B
WHERE A.CHILD = B.CHILD;
END;
END_PROC;
ngalemmo
ngalemmo

Posts : 3000
Join date : 2009-05-15
Location : Los Angeles

http://aginity.com

Back to top Go down

Populating the Bridge table (Tsql) Empty Re: Populating the Bridge table (Tsql)

Post  epekarik Thu Sep 06, 2012 10:49 pm

Wow. This is so much nicer than the examples on the internet using MS SQL "WITH" clause and/or "connect by prior " ORACLE solution. This solution is ANSI SQL standard.

One comment. Should the "(SELECT CHILD, LEVEL FROM NGTREE WHERE PARENT = -1 ) B" be "(SELECT CHILD, DISTANCE FROM NGTREE WHERE PARENT = -1 ) B" ? There isn't a column LEVEL in the NGTREE table.

Thank you!
Endre
epekarik
epekarik

Posts : 8
Join date : 2012-07-27
Age : 60
Location : Cincinnati, OH

Back to top Go down

Populating the Bridge table (Tsql) Empty Re: Populating the Bridge table (Tsql)

Post  ngalemmo Fri Sep 07, 2012 1:25 am

Yep.
ngalemmo
ngalemmo

Posts : 3000
Join date : 2009-05-15
Location : Los Angeles

http://aginity.com

Back to top Go down

Populating the Bridge table (Tsql) Empty Re: Populating the Bridge table (Tsql)

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum