Showing posts with label hierarchical. Show all posts
Showing posts with label hierarchical. Show all posts

Friday, March 23, 2012

Question about Nested Set Model - Hierarchical Data in SQL Server 2000

Hi,

I would like to convert my adjancey set model to a nested set model with a left and right.

I understood this part.

Root Node is always with a a Left = 1, right = 2*(Select Count(*) from Table)

How do the leaf node get the left and right numbers.

I was reading an article of Joe Celko regarding the Nested Sets in SQL and I could not understand how do the employee below Albert(who is top level manager) get their node numbers.

Adjanceny Model
--
Emp Boss

Albert Null
Bert Albert
Chuck Albert
Donna Chuck
Eddie Chuck
Fred Chuck

Nested Set Model
-
Emp Left Right
Albert 1 12
Bert 2 3
Chuck 4 11
Donna 5 6
Eddie 7 8
Fred 9 10

If anyone could kindly explain this to me, then I can try converting my table
to a nested set model to prevent recursion.

Thanks.

set nocount on
create table #chain
( seq integer not null primary key,
emp char (20),
chain varchar (20),
lft integer,
rgt integer
)

declare @.begDt datetime set @.begDt = getdate();

with organization (depth, emp, boss, chain)
as
( select 1 as depth,
emp,
boss,
cast(rtrim(emp) as varchar (400))
from tree
where boss is null
union all
select a.depth + 1 as depth,
b.emp,
b.boss,
cast (a.chain + ', ' + rtrim(b.emp) as varchar(400))
from organization a
inner join tree b
on a.emp = b.boss
)
insert into #chain (seq, emp, chain)
select row_number () over (order by chain) as seq,
emp,
chain
from organization

update #chain
set lft = 1 + 2 * ( select count(*) from #chain b
where b.seq < a.seq
)
- ( select count(*) from #chain c
where c.seq < a.seq
and ( c.chain = rtrim(c.emp) or
a.chain like '%, ' + rtrim(c.emp) + '%'
)
)
from #chain a

update #chain
set rgt = lft + 1
+ 2 * ( select count(*) from #chain b
where b.seq <> a.seq
and ( b.chain like '%, ' + rtrim(a.emp) + '%' or
a.seq = 1
) )
from #chain a


print ' '
select datediff (ms, @.begDt, getdate()) as [Elapsed Time]

select emp, lft, rgt from #chain

-- - Output: -

-- emp lft rgt
-- -- -- --
-- Albert 1 12
-- Bert 2 3
-- Chuck 4 11
-- Donna 5 6
-- Eddie 7 8
-- Fred 9 10


-- Elapsed Time
--
-- 46


go

drop table #chain
go

|||

Thanks for the post. Can you please let me know the structure of your table Organization so that I can re-create it.

When I run the code at my SQL Server, I see the 2 errors:

Incorrect syntax near the keyword 'with'.

at the line:

with organization (depth, emp, boss, chain)

So I guess that that I do not have the Organization table and hence the error.

2nd error:

'row_number' is not a recognized function name.

at the line below:

select row_number () over (order by chain) as seq,

THanks.

Tuesday, March 20, 2012

question about hierarchical dimensions

Hi,

I have a cube that has a "Cost Centre" dimension and a "Product" dimension. Both of these are hierarchical and have multiple levels. I've been asked to write a report which will show a breakdown by "product" & "cost centre" for a certain measure. I wrote the following query which does what I wanted...

SELECT
NON EMPTY { Hierarchize([Cost Centre].[L01 Cost Centre Name].members) } ON COLUMNS,
NON EMPTY { Hierarchize([Product].[High Level Product Name].members) } ON ROWS FROM [MyCube]
WHERE ([Measures].[Costs])

I've now been asked to show the cost centes in groups which are not in the hierarchy. For example, lets say that I have the following Cost Centres members in level 2 of my dimension...

- Level 1 Cost Centre
- Debt Markets Origination PMO & Securitisation
- Other Debt Market
- Flow Credit Trading
- Other T&IP
- Aviation Capital
- Project & Export Finance

I've been told that the cost centres belong to another level not defined in the hierarchy...

- Level 1 Cost Centre
- Origination
- Debt Markets Origination PMO & Securitisation
- Other Debt Market
- Sales
- Flow Credit Trading
- Other T&IP
- Banking
- Aviation Capital
- Project & Export Finance

My question is, if "Origination", "Sales" and "Banking" don't exist in my Cost Centre hierarchy, is it possible to create them in AS somehow or is it possible to select them with MDX somwehow?

Thanks for any help, I hope my question makes sense.

Lachlan

What you asked for is the same as grouping pivot table data inside Excel. Under the hood, Excel creates a session cube with grouping dimensions.

|||

Wow thats sounds complicated but it looks like it's the answer. It might be easier for me to just insert the new level into the dimension data itself, if I am allowed to do that :)

Thanks for your help.

Lachlan