集合函數可從維度、階層、層級,或透過遍歷這些物件中成員的絕對與相對位置,並以多種方式構造集合。
集合函數,如同成員函數和元組函數,對於協商分析服務中的多維結構至關重要。 集合函數對於從多維表達式(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]
另一個常用函數是 交叉連接(Crossjoin,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)功能類似於子(Children)功能,但更強大。 它會回傳一個或多個層級成員的後代:
SELECT
[措施]。[網路銷售金額]
ON Columns,
回傳包含曆年以下所有日期的集合
2004 年在日期維度日曆階層中的情況
後裔(
[日期]。[行事曆]。[曆年]。&[2004年]
, [日期]。[行事曆]。[日期])
ON 划船
摘自[冒險作品]
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]