本文介绍了一些经常编写的MDX语句的写法。以下MDX语句可以在SSAS的示例库:Adventure Works中运行。
例子模型
以下的MDX中用到的Hierarchy如下:
百分比
1)某个子项占总体的百分比。比如:每种Product的销售额占所有Product销售额的百分比。
WITH MEMBER [Measures].[Sale Amount Ratio] AS
\'[Measures].[Internet Sales Amount]/([Measures].[Internet Sales Amount], [Product].[Product Categories].[All])\' , FORMAT_STRING = \'0.00%\'
SELECT
{[Measures].[Internet Sales Amount], [Measures].[Sale Amount Ratio]} ON 0,
NON EMPTY [Product].[Product Categories].[Product Name].Members ON 1
FROM [Adventure Works]
2)某个子项占其父项的百分比。比如:每种Product的销售额占其所属的SubCategory销售额的百分比。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER [Measures].[Sale Amount Ratio] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
\'[Measures].[Internet Sales Amount]/
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
([Measures].[Internet Sales Amount], [Product].[Product Categories].CurrentMember.Parent)\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
, FORMAT_STRING = \'0.00%\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], [Measures].[Sale Amount Ratio]} ON 0,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
NON EMPTY CROSSJOIN([Product].[Subcategory].[Subcategory].Members,
[Product].[Product Categories].[Product Name].Members) ON 1
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
3)某个子项占其祖先的百分比。比如:每种Product的销售额占其所属的Category销售额的百分比。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER [Measures].[Sale Amount Ratio] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
\'[Measures].[Internet Sales Amount]/
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
([Measures].[Internet Sales Amount],
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
ANCESTOR([Product].[Product Categories].CurrentMember, [Product].[Product Categories].[Category]))\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
, FORMAT_STRING = \'0.00%\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], [Measures].[Sale Amount Ratio]} ON 0,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
NON EMPTY CROSSJOIN([Product].[Category].[Category].Members, [Product].[Product Categories].[Product Name].Members) ON 1
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
分配、分摊数量
1)根据一个Measure值来分配数量。比如:按照每种Product占总体的销售额多少来分摊成本。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER [Measures].[Product Cost] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
\'([Measures].[Internet Total Product Cost], [Product].[Product Categories].[All])*
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Measures].[Internet Sales Amount]/
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
([Measures].[Internet Sales Amount], [Product].[Product Categories].[All])\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
, FORMAT_STRING = \'0.00\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], [Measures].[Product Cost]} ON 0,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
NON EMPTY [Product].[Product Categories].[Product Name].Members ON 1
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
2)根据一个Hierarchy来分配数量。比如:在Product Hierarchy中计算每种Category的成本的时候,可以根据每种Category下有多少个产品来进行分配。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER [Measures].[Product Cost] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
\'([Measures].[Internet Total Product Cost], [Product].[Product Categories].[All])/
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
Count(
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
Descendants (
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Product].[Product Categories].CurrentMember,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Product].[Product Categories].[Product Name],
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELF
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
),
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
INCLUDEEMPTY
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
)\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
, FORMAT_STRING = \'0.00\'
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], [Measures].[Product Cost]} ON 0,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
NON EMPTY [Product].[Product Categories].[Category].Members ON 1
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
平均值
1)简单平均值。比如:计算一个月中每天平均的销售额是多少。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER Measures.[Avg Gross Profit Margin] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Measures].[Internet Sales Amount]/
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
COUNT(Descendants([Ship Date].[Fiscal].CurrentMember, [Ship Date].[Fiscal].[Date]), INCLUDEEMPTY)
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], Measures.[Avg Gross Profit Margin]} ON COLUMNS,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Ship Date].[Fiscal].[month].Members ON ROWS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
2)加权平均值。没有想到好的例子。
基于时间的计算
1)同比和环比。比如:今年每月的销售额和去年同期相比的变化
这里要补充的是,在同比MDX中,采用COUSIN或ParallelPeriod都可以,但是采用ParallelPeriod更好一些。
2)累计到当前的统计。比如:得到一年中每一个月的累计销售额。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER Measures.[Additive Internet Sales Amount] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SUM(
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
PeriodsToDate([Ship Date].[Fiscal].[Fiscal Year],[Ship Date].[Fiscal].CurrentMember),
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Measures].[Internet Sales Amount]
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
)
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], Measures.[Additive Internet Sales Amount]} ON COLUMNS,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Ship Date].[Fiscal].[month].Members ON ROWS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]
3)移动平均值。比如:计算一种Category过去三个月的平均销售额合计。
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
WITH MEMBER Measures.[Average Internet Sales Amount] AS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
AVG(LastPeriods(3, [Date].[Calendar].CurrentMember),
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
[Measures].[Internet Sales Amount])
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
SELECT
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
{[Measures].[Internet Sales Amount], Measures.[Average Internet Sales Amount]} ON COLUMNS,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
NON EMPTY ([Product].[Product Categories].[Category].Members,
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
DESCENDANTS([Date].[Calendar].[Calendar Year].&[2002], [Date].[Calendar].[Month], SELF)
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
) ON ROWS
![](https://image.cha138.com/20210612/d9ef93abfb334daeb8db412f43354ec2.jpg)
FROM [Adventure Works]