Použití funkcí sady

Funkce množiny získává množinu z dimenze, hierarchie, úrovně nebo procházením absolutních a relativních poloh členů v těchto objektech a konstruuje množiny různými způsoby.

Funkce množin, jako jsou členové funkce a n-tice, jsou nezbytné pro vyjednávání s vícerozměrnými strukturami v analytických službách. Funkce množin jsou také nezbytné pro získání výsledků z dotazů Multidimensional Expressions (MDX), protože množinové výrazy definují osy MDX dotazu.

Jednou z nejběžnějších funkcí množin je funkce Members (Set) (MDX), která získává množinu obsahující všechny členy z určité dimenze, hierarchie nebo úrovně. Následuje příklad jeho použití v dotazu:

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]  

Další běžně používanou funkcí je funkce Crossjoin (MDX). Vrací množinu n-tic reprezentujících kartézský součin množin vložených do ní jako parametry. V praktickém smyslu tato funkce umožňuje vytvářet "vnořené" nebo "křížově tabulované" osy v dotazech:

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]  

Funkce Descendants (MDX) je podobná funkci Dětí , ale je výkonnější. Vrací potomky libovolného člena na jedné nebo více úrovních hierarchie:

SELECT

[Opatření]. [Částka internetového prodeje]

ON Columns,

Vrací sadu obsahující všechna data pod kalendářním rokem

2004 v kalendářní hierarchii dimenze Datum

POTOMCI (

[Date]. [Kalendář]. [kalendářní rok].&[2004]

, [Datum]. [Kalendář]. [Datum])

ON řady

Z [Adventure Works]

Funkce Order (MDX) vám umožňuje uspořádat obsah množiny vzestupně nebo sestupně podle konkrétního číselného výrazu. Následující dotaz vrací stejné členy na řádcích jako předchozí dotaz, ale nyní je řadí podle míry 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]  

Tento dotaz také ilustruje, jak lze množinu vrácenou z jedné funkce množiny, Descendants, předat jako parametr jiné funkci množiny, Order.

Filtrování množiny podle určitých kritérií je velmi užitečné při psaní dotazů, a k tomuto účelu můžete použít funkci Filter (MDX), jak je ukázáno v následujícím příkladu:

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]  

Existují i jiné, sofistikovanější funkce, které umožňují filtrovat množinu jinými způsoby. Například následující dotaz ukazuje, že funkce TopCount (MDX) vrací top n položek v množině:

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]  

Nakonec je možné provádět řadu operací s logickými množinami pomocí funkcí jako Intersect (MDX),Union (MDX) a Except (MDX). Následující dotaz ukazuje příklady těchto dvou funkcí:

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]