/  12
 
 
MDX Examples using AdventureWorksDW DataTom Tuduc
This tutorial shows common MDX expressions and functions using the sampleAdventureWorksDW data in SQL Server 2005 to complement the MDX Language Reference onmsdn:http://msdn.microsoft.com/en-us/library/ms145595.aspx. This tutorial also includes aquick reference of all operators and functions in the appendixes.
Why MDX?
MDX (Multi Dimension Expression) and SQL (Structure Query Language) share a similarity:both have features of tuples calculus and first order predicate calculus. While a SQL returns atable of columns and rows (two dimensions), a MDX query returns a multi dimension cubes.MDX provides syntax for working with data in multi dimension. It also enables the presenting of cubes in Excel-like pivoting format. The following comparison of a simplest MDX query and itslengthy SQL equivalence quickly shows MDX's usefulness:
SELECT [Dim Reseller].[Year Opened].MEMBERS ON COLUMNS,[Dim Product].[English Product Subcategory Name].MEMBERS ON ROWSFROM [Adventure Works DW]
 
 
EQUIVALENT SQL
SELECT dbo.DimProductSubcategory.EnglishProductSubcategoryName,dbo.DimReseller.YearOpened,dbo.FactResellerSales.OrderQuantityINTO dbo.test1FROM dbo.DimReseller,dbo.FactResellerSales,dbo.DimProduct,dbo.DimProductSubcategoryWHERE dbo.DimProduct.ProductKey = dbo.FactResellerSales.ProductKeyand dbo.DimProduct.ProductSubcategoryKey = dbo.DimProductSubcategory.ProductSubcategoryKeyand dbo.FactResellerSales.ResellerKey = dbo.DimReseller.ResellerKeySELECT dbo.test1.EnglishProductSubcategoryName, dbo.test1.YearOpened, SUM(dbo.test1.OrderQuantity)FROM dbo.test1GROUP BY dbo.test1.EnglishProductSubcategoryName, dbo.test1.YearOpenedORDER BY dbo.test1.EnglishProductSubcategoryName, dbo.test1.YearOpened
The equivalent SQL query still has to be pivoted in Excel to show the years horizontally.
MDX Statements Anatomy
 
A MDX statement consists of clauses (i.e. SELECT, FROM, WHERE, etc…). Clauses can have operators (i.e. *, =, <, etc…)and functions (i.e. crossjoint, lastChild, covariance, etc...). See the complete list of functions and operators in the APPENDIXESbelow.
MDX EXAMPLES1.
 
Using WITH and tuples
WITH MEMBER Measures.Profit AS[Measures].[Sales Amount] - [Measures].[Total Product Cost]MEMBER Measures.ProfitPercent AS Measures.Profit/[Measures].[Total Product Cost], FORMAT_STRING ='#.#%'SELECT{Measures.Profit,Measures.ProfitPercent} ON COLUMNS,{( [Dim Product].[Dim Product Category].[1],[Dim Time].[CalendarYear].[2001] ),( [Dim Product].[Dim Product Category].[1],[Dim Time].[CalendarYear].[2002] ),( [Dim Product].[Dim Product Category].[2],[Dim Time].[CalendarYear].[2001] ),( [Dim Product].[Dim Product Category].[2],[Dim Time].[CalendarYear].[2002]) } ON ROWSFROM[Adventure Works DW]

Share & Embed

More from this user

Add a Comment

Characters: ...

This document has made it onto the Rising list!