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

时间:2018-06-27 10:25:29

标签: ssas mdx

我正在尝试编写一个我认为与SQL中的where exists类似的MDX查询。我们有Files,它们是由SubFiles组成的,每个SubFile都在Location中。 Locations有一个维度,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(在{{1}中没有) }或LocA)。它总共有4 LocD

我需要实现的是,对于给定的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 SubFiles all FileA,因为它们在FileC中至少有1个SubFile,而由于LocC中没有FileB,因此未返回SubFiles

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

1 个答案:

答案 0 :(得分: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]