如何跨另一个层次结构执行MDX成员减法?

时间:2016-01-05 01:47:02

标签: sql-server ssas mdx

我有一个包含客户和产品的多维数据集。每个客户持有不同的产品组合,每个产品的数量不同。

我想计算一个客户与另一个客户之间在每个产品中持有的金额之间的差异(减法)。

我正在使用SQL Server Analysis Services 2014. AdventureWorksDW2014的一个示例是:

select
{[Customer].[Country].&[Australia],[Customer].[Country].&[Canada]} on columns,
non empty ([Product].[Category].members, [Measures].[Internet Sales Amount]) on rows
from 
[Adventure Works]

这会生成以下输出:

                Australia       Canada
All Products    $9,061,000.58   $1,977,844.86
Accessories     $138,690.63     $103,377.85
Bikes           $8,852,050.00   $1,821,302.39
Clothing        $70,259.95      $53,164.62

但我想获得的是

                Australia       Canada          (Australia - Canada)
All Products    $9,061,000.58   $1,977,844.86   $7,083,155.72
Accessories     $138,690.63     $103,377.85     $35,312.78
Bikes           $8,852,050.00   $1,821,302.39   $7,030,747.61
Clothing        $70,259.95      $53,164.62      $17,095.33

理想情况下,这不仅可以在MDX中执行,还允许用户选择任意两个任意客户进行比较。

这是我的第一个MDX问题,所以请告诉我它是否应该以不同方式构建。

1 个答案:

答案 0 :(得分:2)

以下是所需输出的MDX查询:

WITH MEMBER Australia AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Australia])}), FORMAT_STRING = "#,#.##"
MEMBER Canada AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Canada])}), FORMAT_STRING = "#,#.##"
MEMBER [Australia - Canada] AS
[Australia] - [Canada]
SELECT
{[Australia], [Canada], [Australia - Canada]} ON COLUMNS,
NON EMPTY {[Product].[Category].MEMBERS} ON ROWS
FROM [Adventure Works]