MDX Learning
Wisdom Guo
MDX Basics
Concept
MDX is a language that expresses selections, calculations, and some metadata definitions against an Online Analytical Processing (OLAP) database, and provides some capabilities for specifying how query results are to be represented.
Visit Results of MDX
There are several different APIs that support MDX, including Object Linking and Embedding Data Base for Online Analytical Processing (OLE DB for OLAP), ADO MD, ADOMD.Net, XMLA (XML for Analysis), the Hyperion Essbase C and Java APIs, and the Hyperion ADM API.
MDX Query
Sample:
SELECT
{ [Measures].[Dollar Sales], [Measures].[Unit Sales] }
on columns,
{ [Time].[Q1, 2005], [Time].[Q2, 2005] }
on rows
FROM [Sales]
WHERE ([Customer].[MA])
Result:
Figure 1-1 A very simple data grid to retrieve.
Use number for axis:
The phrase on axis(n) indicates that those members should be put on the axis numbered n. The names used so far are synonyms for these numbers:
0 Columns
1 Rows
2 Pages
3 Chapters
4 Sections
Case sensitive
MDX is neither case-sensitive nor line-oriented.
SELECT [Dim Customer].[City].[Ascheim] on columns,
[Dim customer].[Gender].[all] on rows
from [adventure works dw]
where measures.[amount]
------------------------
SELECT {[Dim Customer].[Gender].&[M]
,[Dim Customer].[Gender].&[F]}
on COLUMNS
,[Measures].[Fact Internet Sales Count]
ON ROWS
from [adventure works dw]
--
SELECT
{ [Measures].[Sales Amount],
[Measures].[Tax Amount] } ON COLUMNS,
{ [Date].[Fiscal Time].[Fiscal Year].&[2002],
[Date].[Fiscal Time].[Fiscal Year].&[2003] } ON ROWS
FROM [Adventure Works]
WHERE ( [Sales Territory].[Southwest] )
------the colon usage---------------
select {[Dim Time].[Calendar Year].&[2001]: [Dim Time].[Calendar Year].&[2004]} on rows,
{[Dim Product].[Color].[Color]} on columns
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
--- the .members usage
select {[Dim Time].[Calendar Year].&[2001]} on rows,
{[Dim Product].[Color].members} on columns
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
-- the usages of chidren
select {[Dim Time].[Calendar Year].&[2001]} on rows,
{[Dim Product].[Color].&[Blue].children} on columns
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
-- the usages of descedents
select {descendants([Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter].&[1] ,[Dim Time].[Fiscal Quarter - Month Number Of Year].[Month Number Of Year],self)} on rows,
{[Dim Product].[Color].members} on columns
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
--self only the same level as [Month Number Of Year]
--self and before will start up from [Month Number Of Year] to the top.
select
NON EMPTY
{
descendants
(
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter].&[1]
,[Dim Time].[Fiscal Quarter - Month Number Of Year].[Month Number Of Year]
,self_and_before
)
}
on rows,
{
[Dim Product].[Color].members
}
on columns
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
--NON Empty : to remove the emtpy row in the returned results.
--------comment usage----------
/*
This is a comment
*/
//comment
--comment
--------Learn tuples--------
select
{--sets
( --tuples
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter]
,[Dim Customer].[Gender].&[m]
) ,
(
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Month Number Of Year]
,[Dim Customer].[Gender].&[f]
)
}
on columns,
{
[Dim Product].[Color].members
}
on rows
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
------zero axis--
select from [adventure works dw]
select from [adventure works dw]
where {--sets
( --tuples
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter]
,[Dim Customer].[Gender].&[m]
)
}
---some more function: filter, crossjion(),
select
crossjoin
(
[Dim Product].[Dim Product Category].&[1]
,
crossjoin
(
{--sets
( --tuples
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter]
)
}
,{--sets
(
[Dim Customer].[City].&[Alpine]
)
}
)
)
on columns,
{
[Dim Product].[Color].members
}
on rows
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
---------use asterisk-- for cross join-----
select
{[Dim Product].[Dim Product Category].&[1]}
*
{--sets
( --tuples
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter]
)
}
*{--sets
(
[Dim Customer].[City].&[Alpine]
)
}
on columns,
{
[Dim Product].[Color].members
}
on rows
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
---filter()
select
filter
(
{[Dim Product].[Dim Product Category].&[1]}
*
{--sets
( --tuples
[Dim Time].[Fiscal Quarter - Month Number Of Year].[Fiscal Quarter]
)
}
*{--sets
(
[Dim Customer].[City].&[Alpine]
)
}
, [Measures].[Fact Sales Quota Count] > 40 --boolean condition
)
on columns,
{
[Dim Product].[Color].members
}
on rows
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
--order----
select
Order
(
[Dim Product].[Dim Product Category].&[1]
,
BDESC
) on columns ,
{
[Dim Product].[Color].members
}
on rows
from [adventure works dw]
where [Measures].[Fact Sales Quota Count]
-------get properties-
select
{--sets
(
[Dim Product].[Dim Product].[color]
)
}
properties [Dim Product].[Dim Product].[color]
on columns,
{
[Dim Product].[English Product Name].members
}
on rows
from [adventure works dw]
_________________________________________
To be continued...
PS: lately, our team gets a project related to BI, we need cleanse, ETL the raw data files to SQL Server Dataware house by SSIS.
So I take this chance to learn on SSIS, and MDX how to build the Cube, how to query the Cube by MDX sentence.
I find it's interesting.
wisdom