2014-03-13 22 views
0

我們有一個工作人員維度,爲管理員(父 - 子關係)提供了一個自我引用,我們在其上構建了層次結構。SCD類型2與父子級別聚合問題

DimStaff表:

| SurrogateKey | BusinessKey | Employee Name | ManagerBusinessKey | StartDate | EndDate | 
|  1  |  1  | Manager1 |  NULL  | 2013-01-01 | 2099-01-01| 
|  2  |  2  | Manager2 |  NULL  | 2013-01-01 | 2099-01-01| 
|  3  |  3  | Employee1 |  1   | 2013-01-01 | 2014-01-01| 
|  4  |  3  | Employee1 |  2   | 2014-01-02 | 2099-01-01| 

事實表:現在

| StaffKey | DateKey | Measure1 | 
| 3  | 20130405 | 10  | 
| 4  | 20140203 | 20  | 

,與此數據集作爲一個例子,要求是

1-能夠通過向下鑽取層次結構

Manager1 
    -> Employee1 
      -> Measure1=10 
Manager2 
    -> Employee1 
      -> Measure1=20 

2 - 當選擇了一個人

Employee1 -> Measure1=30 

我們怎麼能去這樣做合計值對每個層級? (問題在於我們構建了它,但第二個要求不起作用,因爲cube接受Employee1的兩個狀態作爲兩個單獨的實體並且不會聚合它們。)

+0

應的層次結構不能建立在代理鍵? I. e。將一列'ManagerSurrogateKey'添加到維度表並使用它來定義自我引用。 – FrankPl

回答

0

我聽起來像您的Employee Name已在屬性上定義它使用SurrogateKey作爲KeyColumns屬性。我將定義一個新的屬性,它具有KeyColumns的BusinessKey和NameColumn的Employee Name。

0

嘗試下面的SQL得到的答案

DECLARE @DimStaff table(
SurrogateKey INT, 
BusinessKey INT, 
EmployeeName Varchar(30), 
ManagerBusinessKey INT, 
StartDate DATE, 
EndDate DATE 
); 

Insert into @DimStaff 
    (SurrogateKey, BusinessKey, EmployeeName, ManagerBusinessKey, StartDate, EndDate) 
Values 
    (1,1,'Manager1', NULL, '2013-01-01', '2099-01-01'), 
    (2,2,'Manager2', NULL, '2013-01-01', '2099-01-01'), 
    (3,3,'Employee1', 1, '2013-01-01', '2014-01-01'), 
    (4,3,'Employee1', 2, '2014-01-02', '2099-01-01') 

DECLARE @FactTable table(
StaffKey INT, 
DateKey DATE, 
Measure1 INT 
); 

INSERT INTO @FactTable 
    (StaffKey, DateKey, Measure1) 
Values 
    (3, '2013-04-05', 10), 
    (4, '2013-02-03', 20) 

select t1.EmployeeName as Manager, t2.EmployeeName as Employee, ft.Measure1 as Measure from @DimStaff t1 
inner join @DimStaff t2 on t1.BusinessKey = t2.ManagerBusinessKey 
inner join @FactTable ft on ft.StaffKey = t2.SurrogateKey 

select t1.EmployeeName as Employee, sum (ft.Measure1) as TotalMeasure from @DimStaff t1 
inner join @FactTable ft on ft.StaffKey = t1.SurrogateKey 
group by t1.EmployeeName