使用 Set 函式
set 函式會從維度、階層、層級或透過周游這些對象中成員的絕對和相對位置,以各種方式建構集合,來擷取集合。
集合函式,例如成員函式和 Tuple 函式,對於交涉 Analysis Services 中找到的多維度結構至關重要。 Set 函式對於從多維度表達式 (MDX) 查詢取得結果也很重要,因為集合表達式會定義 MDX 查詢的座標軸。
最常見的集合函式之一是 Members (Set) (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]
另一個常用的函式是 Crossjoin (MDX) 函式。 它會傳回一組 Tuple,代表傳遞至它的集合做為參數的笛卡兒乘積。 實際上,此函式可讓您在查詢中建立「巢狀」或「交叉選取」軸:
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) 函式類似於 Children 函式,但功能更強大。 它會傳回階層中一或多個層級之任何成員的子系:
SELECT
[量值]。[因特網銷售金額]
ON 數據行,
傳回集合,其中包含行事歷年度底下的所有日期
日期維度行事歷階層中的 2004
子系(
[日期]。[行事歷]。[Calendar Year].&[2004]
、 [Date]。[行事歷]。[日期])
ON 數據列
FROM [Adventure Works]
Order (MDX) 函式可讓您根據特定的數值表示式,以遞增或遞減順序排序集合的內容。 下列查詢會傳回與上一個查詢相同的數據列成員,但現在會依 Internet Sales Amount 量值來排序這些成員:
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]
此查詢也會說明如何從一個集合函式子代子系傳回的集合,當做參數傳遞至另一個 set 函式 Order。
根據特定準則篩選集合在撰寫查詢時非常有用,因此您可以使用 Filter (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]
最後,您可以使用 Intersect (MDX)、Union (MDX) 和 Except (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]