如何编写MDX查询以获取分组结果

时间:2015-12-09 10:46:50

标签: sql-server mdx olap saas

我有多维数据集聚合数据,我需要记录每个2位数邮政编码的记录数。

附加图像显示了我的多维数据集层次结构和度量。

OlapCube

我应用了以下查询:

WITH MEMBER [Measures].NoOfConsignments as Count(([Consignment].[Target Address Name].[Target Address Name]))
select filter([Consignment].[Distribution Area].[Zip2], [Consignment].[Distribution Area].[Target Address Country] = "94") on rows,
{[Measures].NoOfConsignments} on columns
from [RebellOlap]
where ({[Consignment].[Distribution Area].[Target Address Country].&[94]})

但是引发了错误:

“分布区域层次结构已出现在Axis1轴”

我按照以下方式重新构建并制定了子选择查询:

WITH MEMBER [Measures].NoOfConsignments as Count(([Consignment].[Target Address Name].[Target Address Name]))
Select
NON EMPTY [Measures].NoOfConsignments on columns,
NON EMPTY [Consignment].[Distribution Area].[Zip2] on rows
FROM  ( 
    SELECT {[Consignment].[Distribution Area].[Zip2],[Consignment].[Distribution Area].[Target Address Country].&[94]}
    FROM [RebellOlap] 
   )

但它也给我带来了“歧义错误”。

我需要以下列方式输出

enter image description here

修改

德国的AllConsignments

enter image description here

德国境内针对特定邮政编码的所有货物

enter image description here

2 个答案:

答案 0 :(得分:1)

我刚刚为所有邮政编码应用了过滤器,并将“Range”作为运算符引入了“过滤器表达式”并且它有效!!

SELECT NON EMPTY { [Measures].[ConsignmentCount] } ON COLUMNS, 
NON EMPTY { ([Consignment].[Distribution Area].[Zip2].ALLMEMBERS ) } ON ROWS 
FROM ( SELECT ( [Consignment].[Distribution Area].[Zip2].&[94]&[0]&[01] : [Consignment].[Distribution Area].[Zip2].&[94]&[9]&[99] ) ON COLUMNS 
FROM [RebellOlap])

答案 1 :(得分:0)

根据您的修改,我觉得以下内容应该适合您:

WITH MEMBER Measures.NumConsignment as
COUNT
     (
      NonEmpty
             (
              [Consignment].[Target Address Name].[Target Address Name].MEMBERS, 
              ([Consignment].[Distribution Area].CURRENTMEMBER, Measures.[Num. Consignments])
             )
     )

SELECT [Consignment].[Distribution Area].[Zip2].MEMBERS on 1,
Measures.NumConsignment ON 0
FROM 
(
 SELECT [Consignment].[Distribution Area].[Target Address Country].&[94] ON 0
 FROM [RebellOlap]
)

虽然其余代码非常标准,但有一部分可能需要解释。

      NonEmpty
             (
              [Consignment].[Target Address Name].[Target Address Name].MEMBERS, 
              ([Consignment].[Distribution Area].CURRENTMEMBER, Measures.[Num. Consignments])
             )

这将返回当前 zip的一组目标地址,货物数量不为空(即<> 0)