Korzystanie z funkcji Set

Funkcja zbioru pobiera zbiór z wymiaru, hierarchii, poziomu lub poprzez przechodzenie przez absolutne i względne położenia członków w obrębie tych obiektów, konstruując zbiory na różne sposoby.

Funkcje zbiorowe, takie jak funkcje członkowskie i funkcje krotki, są niezbędne do negocjowania wielowymiarowych struktur występujących w usługach analitycznych. Funkcje zbiorowe są również niezbędne do uzyskania wyników z zapytań Multidimensional Expressions (MDX), ponieważ wyrażenia zbiorowe definiują osie zapytania MDX.

Jedną z najczęściej spotykanych funkcji zbioru jest funkcja Members (Set) (MDX), która pobiera zbiór zawierający wszystkie elementy z określonego wymiaru, hierarchii lub poziomu. Poniżej znajduje się przykład jego zastosowania w zapytaniu:

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]  

Inną powszechnie używaną funkcją jest funkcja Crossjoin (MDX). Zwraca zbiór krotek reprezentujących iloczyn kartezjański zbiorów przekazanych do niego jako parametrów. W praktyce funkcja ta pozwala tworzyć osie "zagnieżdżone" lub "crosstabowane" w zapytaniach:

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]  

Funkcja Descendants (MDX) jest podobna do funkcji Dzieci , ale jest potężniejsza. Zwraca potomków dowolnego członka na jednym lub kilku poziomach hierarchii:

SELECT

[Środki]. [Kwota sprzedaży internetowej]

ON Columns,

Zwraca zestaw zawierający wszystkie daty poniżej roku kalendarzowego

2004 rok w hierarchii kalendarzowej wymiaru Daty

POTOMKOWIE(

[Date]. [Kalendarz]. [Rok kalendarzowy].&[2004]

, [Data]. [Kalendarz]. [Date])

ON Rows

Z [Adventure Works]

Funkcja Order (MDX) pozwala uporządkować zawartość zbioru w kolejności rosnącej lub malejącej zgodnie z określonym wyrażeniem liczbowym. Następujące zapytanie zwraca te same elementy wierszy co poprzednie zapytanie, ale teraz uporządkowuje je według miary 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]  

To zapytanie ilustruje również, jak zbiór zwrócony z jednej funkcji zbioru, Descendants, może być przekazywany jako parametr do innej funkcji zbioru, Order.

Filtrowanie zbioru według określonych kryteriów jest bardzo przydatne podczas pisania zapytań, dlatego można użyć funkcji Filter (MDX), jak pokazano w poniższym przykładzie:

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]  

Istnieją inne, bardziej zaawansowane funkcje, które pozwalają filtrować zbiór w inny sposób. Na przykład następujące zapytanie pokazuje, że funkcja TopCount (MDX) zwraca najwyższe n elementów zbioru:

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]  

Wreszcie możliwe jest wykonanie wielu operacji zbiorów logicznych za pomocą funkcji takich jak Intersect (MDX),Union (MDX) oraz Except (MDX). Poniższe zapytanie pokazuje przykłady tych dwóch ostatnich funkcji:

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]