使用 Set 函数

集合函数通过遍历这些对象中成员的绝对和相对位置,从而从维度、层级、层级中获取集合,并以多种方式构造集合。

集合函数,与成员函数和元组函数一样,对于协商分析服务中的多维结构至关重要。 集合函数对于获取多维表达式(MDX)查询的结果也至关重要,因为集合表达式定义了MDX查询的轴。

最常见的集合函数之一是 成员(集合)(MDX) 函数,它从一个维度、层级或层级检索包含所有成员的集合。 以下是其在查询中的应用示例:

SELECT  
//Returns all of the members on the Measures dimension  
[Measures].MEMBERS  
ON Columns,  
//Returns all of the members on the Calendar Year level of the Calendar Year Hierarchy  
//on the Date dimension  
[Date].[Calendar Year].[Calendar Year].MEMBERS  
ON Rows  
FROM [Adventure Works]  

另一个常用函数是 交叉连接(MDX) 函数。 它返回一组元组,代表作为参数传递的集合的笛卡尔积。 在实际操作中,这个函数可以让你在查询中创建“嵌套”或“交叉表”轴:

SELECT  
//Returns all of the members on the Measures dimension  
[Measures].MEMBERS  
ON Columns,  
//Returns a set containing every combination of all of the members  
//on the Calendar Year level of the Calendar Year Hierarchy  
//on the Date dimension and all of the members on the Category level  
//of the Category hierarchy on the Product dimension  
Crossjoin(  
[Date].[Calendar Year].[Calendar Year].MEMBERS,  
[Product].[Category].[Category].MEMBERS)  
ON Rows  
FROM [Adventure Works]  

后裔(MDX)功能类似于子嗣功能,但更强大。 它返回一个或多个层级成员的后代:

SELECT

[措施]。[Internet Sales Amount]

论柱,

返回包含日历年以下所有日期的集合

2004年在日期维度历法层级中的数据

后代(

[日期]。[日历]。[日历年]。&[2004年]

, [日期]。[日历]。[日期])

安大略省划船

摘自[冒险工作]

Order(MDX)功能允许你根据特定的数值表达式对集合内容进行升序或降序排序。 以下查询返回与上一查询相同的行成员,但现在按互联网销售金额度量排序:

SELECT  
[Measures].[Internet Sales Amount]  
ON Columns,  
//Returns a set containing all of the Dates beneath Calendar Year  
//2004 in the Calendar hierarchy of the Date dimension  
//ordered by Internet Sales Amount  
ORDER(  
DESCENDANTS(  
[Date].[Calendar].[Calendar Year].&[2004]  
, [Date].[Calendar].[Date])  
, [Measures].[Internet Sales Amount], BDESC)  
ON Rows  
FROM [Adventure Works]  

该查询还说明了如何将一个集合函数 Descendants 返回的集合作为参数传递给另一个集合函数 Order。

根据特定条件过滤集合在编写查询时非常有用,为此你可以使用 筛选(MDX) 函数,如下示例所示:

SELECT  
[Measures].[Internet Sales Amount]  
ON Columns,  
//Returns a set containing all of the Dates beneath Calendar Year  
//2004 in the Calendar hierarchy of the Date dimension  
//where Internet Sales Amount is greater than $70000  
FILTER(  
DESCENDANTS(  
[Date].[Calendar].[Calendar Year].&[2004]  
, [Date].[Calendar].[Date])  
, [Measures].[Internet Sales Amount]>70000)  
ON Rows  
FROM [Adventure Works]  

还有其他更复杂的函数,可以让你用其他方式过滤集合。 例如,以下查询显示 TopCount(MDX) 函数返回集合中的前n项:

SELECT  
[Measures].[Internet Sales Amount]  
ON Columns,  
//Returns a set containing the top 10 Dates beneath Calendar Year  
//2004 in the Calendar hierarchy of the Date dimension by Internet Sales Amount  
TOPCOUNT(  
DESCENDANTS(  
[Date].[Calendar].[Calendar Year].&[2004]  
, [Date].[Calendar].[Date])  
,10, [Measures].[Internet Sales Amount])  
ON Rows  
FROM [Adventure Works]  

最后,可以使用诸如交集(MDX)、并集(MDX)非例外(MDX)等函数执行多种逻辑集合运算。 以下查询展示了后两种函数的示例:

SELECT  
//Returns a set containing the Measures Internet Sales Amount, Internet Tax Amount and  
//Internet Total Product Cost  
UNION(  
{[Measures].[Internet Sales Amount], [Measures].[Internet Tax Amount]}  
, {[Measures].[Internet Total Product Cost]}  
)  
ON Columns,  
//Returns a set containing all of the Dates beneath Calendar Year  
//2004 in the Calendar hierarchy of the Date dimension  
//except the January 1st 2004  
EXCEPT(  
DESCENDANTS(  
[Date].[Calendar].[Calendar Year].&[2004]  
, [Date].[Calendar].[Date])  
,{[Date].[Calendar].[Date].&[915]})  
ON Rows  
FROM [Adventure Works]