在维度名称上聚合

时间:2014-05-28 16:53:25

标签: ssas mdx ssas-2008

我正在尝试根据一个维度中成员的名称聚合查询结果。以下内容将返回错误,但这与我的想法非常接近:

with member [Staff].[Staff Hierarchy].x as 
aggregate ([Staff].[Staff Hierarchy].currentmember.name)

select [Measures].[a measure] on 0,
[Staff].[Staff Hierarchy].x on 1 
from 
(
select [Staff].[Staff Hierarchy].allmembers on 0
from [the cube]
)

注意:我基本上试图按名称分组,因为我们在一个人的事实表中有多个记录。由于我们使用的是父子层次结构,因此我不能将人员的名称用作我的维度的属性。

更多细节: 我想做类似于this的事情,但不是“案例”,而是需要员工姓名。这与group by非常相似。

2 个答案:

答案 0 :(得分:1)

OPENQUERY必须是最后的手段,因为它有严重的性能问题。我们发现的替代方案如下:

MEMBER [Staff].[Staff Hierarchy].[Employee number 1] AS
Aggregate ( [Staff].[Staff Hierarchy].&[101], [Staff].[Staff Hierarchy].&[102] )

为所有员工自动创建成员,我们写了这样一个查询:

Declare @mdx varchar(max)
Declare @thisStaffName varchar(500)
declare @thisStaffMember varchar(500)
declare @ThisStaffBusinessKey int
Declare @ThisStaffSurrogateKey int
Declare @TheSet varchar(max)='set CustomEmployeeSet as {[Staff].[Staff Hierarchy].['

set @mdx = 'with member [Staff].[Staff Hierarchy].[' 
Declare  staff_Cursor Cursor for 
select distinct  top 10 BusinessKey from DIM_Staff order by BusinessKey

open staff_cursor

Fetch Next from staff_Cursor into @ThisStaffBusinessKey

while @@FETCH_STATUS = 0
BEGIN
    print 'APN: ' + cast(@ThisStaffBusinessKey as varchar(200))
    select  @thisStaffName = STAFF_HIER_LEVEL_CD + '-' + FIRST_NM + ' ' +LAST_NM + '-' + CAST(BusinessKey AS VARCHAR(200))
    from DIM_Staff 
    where BusinessKey=@ThisStaffBusinessKey 

    set @TheSet =@TheSet + @thisStaffName + '],[Staff].[Staff Hierarchy].['
    set @mdx = @mdx + @thisStaffName + '] as aggregate('

    declare This_Staff_Cursor Cursor for 
    select STAFF_HIST_ROW_ID from DIM_Staff
    where BusinessKey=@ThisStaffBusinessKey


    open This_Staff_Cursor

    Fetch Next from This_Staff_Cursor into @ThisStaffSurrogateKey

    while @@fetch_status =0
    begin
        print 'SHRI: ' + CAST(@ThisStaffSurrogateKey AS VARCHAR(200))
        set @mdx = @mdx + '[Staff].[Staff Hierarchy].&[' + CAST(@ThisStaffSurrogateKey AS VARCHAR(200))
        Fetch Next from This_Staff_Cursor into @ThisStaffSurrogateKey

            SET @mdx = @mdx + '],' 
    END
    set @mdx = left(@mdx, len(@mdx)-1)
    set @mdx = @mdx + ') member [Staff].[Staff Hierarchy].['
    close This_Staff_Cursor
    DEALLOCATE This_Staff_Cursor
    Fetch Next from staff_Cursor into @ThisStaffBusinessKey
END
set @mdx = left(@mdx, len(@mdx)-34)
set @TheSet = left(@TheSet, len(@TheSet)-28)
set @TheSet = @TheSet + '}'
set @mdx = @mdx + ' '+@TheSet
close staff_cursor
deallocate staff_cursor
SELECT @mdx + 'select [the measure] on 0, CustomEmployeeSet on 1 from [the cube] '

答案 1 :(得分:0)

应该在多维数据集中聚合度量。在我们的多维数据集中,如下所示的脚本将为每个员工创建“分组”结果

WITH MEMBER [Measures].[memberName] AS
'[Staff].[Staff Hierarchy].CurrentMember.name'
SELECT 
    {[Measures].[a measure], [Measures].[memberName]} ON COLUMNS,
    [Staff].[Staff Hierarchy].members ON ROWS  //<< is there not a level? [Staff].[Staff Hierarchy].[a Level].members ON ROWS
FROM [the cube]