返回至少有一个存在于同一父级中的所有子级

问题描述 投票:0回答:1

我正在尝试编写一个MDX查询,我认为它类似于SQL中的where exists。我们有Files,由SubFiles制成,每个SubFile都在LocationLocations的维度和Files的维度(包含File -> SubFile层次结构),以及所有SubFiles的度量计数。

所以下面的MDX:

select
    [Location].[Location].members on 0,
    [File].[File].members on 1
from
    [MyCube]

返回类似于:

             | LocA | LocB | LocC | LocD
----------------------------------------
FileA        | null |  2   |   2  | null
FileB        |  1   |  2   | null | null
FileC        | null | null |   1  |  2

这表明,例如,FileASubFiles中有2个LocB,在SubFiles中有2个LocC(在LocALocD中没有)。总共有4个SubFiles

我需要实现的是,对于给定的Location返回所有SubFiles,其中至少一个SubFile在相同的File下是在给定的Location。因此,对于例如给定上面的示例,如果给定位置是LocC,则结果集应该是:

             | LocA | LocB | LocC | LocD
----------------------------------------
FileA        |
   SubFileA1 | null | null |   1  | null
   SubFileA2 | null |   1  | null | null
   SubFileA3 | null |   1  | null | null
   SubFileA4 | null | null |   1  | null
FileC        |
   SubFileC1 | null | null | null |   1
   SubFileC2 | null | null |   1  | null
   SubFileC3 | null | null | null |   1

所以SubFilesFileA的所有FileC都返回,因为他们在SubFile中至少有1个LocC,而FileB没有返回,因为它在SubFiles没有LocC

如何在MDX中实现这一目标?

ssas mdx
1个回答
1
投票

您可以使用Exists function获取文件,然后使用Descendants function添加子文件:

select
    [Location].[Location].members
    on 0,
    Descendants(
        Exists(
            [File].[File -> SubFile].[File].members, 
            {[Location].[Location].[LocC]}
        ), 
        [File].[File -> SubFile].[SubFile],
        SELF_AND_BEFORE
    )
    on 1
from
    [MyCube]
© www.soinside.com 2019 - 2024. All rights reserved.