SSAS - 透视查询结果

时间:2018-06-01 15:15:55

标签: sql sql-server ssas mdx

我必须创建每个多维数据集使用的维度和属性列表(总共5个多维数据集)。有些被多个立方体使用。

使用以下脚本:

SELECT [CUBE_NAME] AS [CUBE],
 [DIMENSION_UNIQUE_NAME] AS [DIMENSION],
 LEVEL_CAPTION AS [ATTRIBUTE]
FROM $system.MDSchema_levels
WHERE level_origin=2
AND LEVEL_NAME <> '(All)'

我能够得到一份我需要的清单:

 CUBE | Dimension | Attribute
 A    | Person    | Name
 A    | Person    | First Name
 A    | Location  | City
 B    | Person    | Name
 B    | Person    | First Name
 C    | Product   | Productname

为了获得更清晰的视图,我正在寻找一种方法将其转为:

 Dimension | Attribute    | Cube A | Cube B | Cube C 
 Person    | Name         | X      | X      | 
 Person    | First Name   | X      | X      | 
 Location  | City         | X      |        | 
 Product   | Productname  |        |        | X

(X表示哪个属性在哪个多维数据集中存在)我知道可以在SQL中对此进行转换,但我不知道如何在MDX中执行此操作。有什么建议/想法吗?

1 个答案:

答案 0 :(得分:2)

您似乎想要条件聚合:

SELECT Dimension, Attribute, 
       MAX(CASE WHEN CUBE = 'A' THEN 'X' END) AS CubeA,
       MAX(CASE WHEN CUBE = 'B' THEN 'X' END) AS CubeB,
       MAX(CASE WHEN CUBE = 'C' THEN 'X' END) AS CubeC
FROM $system.MDSchema_levels
WHERE level_origin=2 AND LEVEL_NAME <> '(All)'
GROUP BY Dimension, Attribute;