【问题标题】:SCD type 2 with parent-child hierarchy aggregation issue具有父子层次聚合问题的 SCD 类型 2
【发布时间】:2014-03-13 16:20:56
【问题描述】:

我们有一个员工维度,其中包含我们在其上构建层次结构的经理(父子关系)的自我引用。

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

我们怎样才能做到这一点? (问题是我们构建了它,但第二个要求不起作用,因为立方体接受 Employee1 的两个状态作为两个单独的实体并且不会聚合它们。)

【问题讨论】:

  • 不应该在代理键上建立层次结构吗? IE。在维度表中添加一列 ManagerSurrogateKey 并使用它来定义自引用。

标签: ssas olap business-intelligence olap-cube scd


【解决方案1】:

听起来您的员工姓名已在使用 SurrogateKey 作为 KeyColumns 属性的属性上定义。我会定义一个新属性,其中 KeyColumns 具有 BusinessKey,NameColumn 具有 Employee Name。

【讨论】:

    【解决方案2】:

    试试下面的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2011-02-25
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多