Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

Reader rating:
Click here to read 1 comment about this article.
Article source: CoDe (2008 Jan/Feb)


Article Pages:  1  2 3 - Next >


The Baker’s Dozen: 13 Tips for Querying OLAP Databases with MDX

MDX is just like Transact-SQL, right? MDX is English-like and therefore easy to learn, right? Different software tools offer MDX generators and therefore I don’t need to learn MDX, right? MDX isn’t that popular, right? Well, just like the punch-line of the old Hertz car rental commercial, “Not exactly.” If your organization uses OLAP databases, you can add great value by knowing how to query OLAP databases using MDX queries. This article will cover common OLAP query requirements and MDX code solutions.

What Is MDX and Why Should I Care?

MDX stands for Multidimensional Expressions. You use it to query OLAP databases. In a nutshell, MDX is to OLAP databases as SQL queries are to relational databases.

So OK-what are OLAP databases? OLAP stands for Online Analytical Processing. OLAP databases primarily consist of OLAP cubes, which store facts (i.e., “measures” such as sales, returns, etc.) and dimensions/dimension hierarchies. An OLAP database is often an aggregation of a relational database; as a result, you can write MDX queries to retrieve key calculations that measure company performance, often with less code than standard SQL.

"
If you want to RANK a result set based on an order, first create an ordered set and then create a calculated member that applies the RANK function against the ordered set. This will usually provide maximum performance.
"

Microsoft created MDX in the late 1990’s. One of the architects for Microsoft Analysis Services, Mosha Pasumansky, invented the MDX language. (See the end of this article for recommended references.) Other vendors have since implemented MDX, such as Essbase by Hyperion Solutions (now owned by Oracle). However, Microsoft continues to add MDX extensions to new versions of Analysis Services.

Because of the nature of OLAP databases, “power-users” can often write MDX code to retrieve data in far fewer lines of code than would be required using SQL. This is a segue into the role that OLAP databases and MDX play in the world of business intelligence.

Over the last few years, Microsoft has made serious advancements in the areas of business intelligence and OLAP databases. Each year, more and more companies use BI and OLAP tools that support MDX. Here are some examples where MDX comes into play:

  • Creating Key Performance Indicators (KPIs).
  • Building SQL Server Reporting Services reports against OLAP databases.
  • Designing custom dashboard output in PerformancePoint Server (for SharePoint deployment).

If you are a SQL developer who is new to MDX, you may look at MDX syntax and conclude that the syntax is very similar. As it turns out, SQL and MDX are very different. Many tools that use MDX will offer designers that generate basic MDX syntax; however, you may still need to modify or customize the generated syntax to address specific requirements.

What’s On the Menu?

Recently, a CoDe Magazine subscriber approached me at a community event and asked me why I always include thirteen tips. I smiled and told him about the famous Saint Nicholas tale of the legendary Van Amsterdam Bakery. (You can read the full story of the Baker’s Dozen at http://www.aaronshep.com/rt/RTE09.html). The story symbolizes the providence of giving as much as you can.

"
Use FILTER inside a CROSSJOIN-not the other way around.
"

So, here are the 13 tips on the menu for this issue:

  • Some basic MDX queries to learn MDX syntax.
  • Sorting result sets with ORDER.
  • Using WITH SET and WITH MEMBER to create sets and calculated members.
  • Filtering result sets with WHERE and FILTER.
  • Assigning a sequential ranking number to result sets with RANK.
  • Retrieving TOP N results using TOPCOUNT, TOPSUM, and TOPPERCENT.
  • Combining complex SET definitions with GENERATE.
  • Using the HEAD and TAIL functions when you need to retrieve the top or bottom data from a result set.
  • Prioritizing calculations using SOLVE_ORDER.
  • Retrieve data by time period using a variety of period functions, such as ParallelPeriod and LastPeriod.
  • Retrieving data in a hierarchy using DESCENDANT.
  • Using the LOOKUP function to retrieve data from multiple OLAP cubes.
  • The Baker’s Dozen Spotlight: Incorporating an MDX query into a SQL Server Reporting Services report, using parameters.

Before I begin, I want to make a special thanks to Mr. Deepak Gupta. Deepak is a SQL Server MVP who monitors many online forums, including the Microsoft public newsgroups. In writing this article, I needed to research some material and post some questions online, and Deepak was able to answer my questions. Deepak, you really know your stuff!

The Data Source

The code for this article uses the OLAP database for AdventureWorks, as shown in Figure 1. If you don’t have the AdventureWorks BI project you can download it from http://www.codeplex.com/MSFTDBProdSamples/Release/ProjectReleases.aspx?ReleaseId=4004. The AdventureWorks BI project contains many common attributes for an OLAP database.

Click for a larger version of this image.

Figure 1: The AdventureWorks Cube.

Tip 1: “MDX Looks Like T-SQL, Tastes Like T-SQL-but…”

Here are some starter queries to help you get a feel for MDX. This first query retrieves sales amount by Customer State. Note the clauses ON COLUMNS, ON ROWS, and FROM [CubeName].

SELECT
   [Measures].[Internet Sales Amount] ON COLUMNS,
   [Customer].[State-Province].CHILDREN ON ROWS
FROM [Adventure Works]

Either from this MDX query, or from the next set of queries, you’ll discover some immediate differences between MDX and SQL:

  • You explicitly state the COLUMNS and ROWS that you want for the result set.
  • You don’t issue JOIN statements.
  • You will usually query from one OLAP cube. In rare instances, you may need to retrieve data from a second cube. Tip 13 covers this with the LOOKUPCUBE function.

When you view the results of the first MDX query, you’ll also notice that the result set contains a row for every state, even those with NULL results. You can use the NON EMPTY statement to filter out NULL results.

-- Filter out NULL results
SELECT
   [Measures].[Internet Sales Amount] ON COLUMNS,
NON EMPTY
   [Customer].[State-Province].CHILDREN ON ROWS
FROM [Adventure Works]

Of course, many MDX queries will need to return multiple measures, broken out by multiple dimensions. To return sales and freight by Product Subcategory within state, you’ll need to wrap curly braces around the column names, and parentheses around the dimensions:

-- Use {} to enclose multiple measures
-- Use () to enclose multiple dimensions

SELECT   {
  [Measures].[Internet Sales Amount],
  [Measures].[Internet Freight Cost] } ON COLUMNS,
NON EMPTY
     ( [Customer].[State-Province].CHILDREN,
      [Product].[Subcategory].CHILDREN ) ON ROWS
 FROM [Adventure Works]

Tip 2: Can I Take Your “Order”?

Most results sets need to be sorted in a particular order. The MDX equivalent of the SQL ORDER BY statement is the ORDER function.

The ORDER function receives three parameters: the set definition to be sorted (i.e., show States and Subcategories down the left), the data to sort on (Sales Amount), and whether to sort in ascending or descending sequence.

 -- Use ORDER  (will sort within customer)
 SELECT   [Measures].[Internet Sales Amount]
  ON COLUMNS,
  NON EMPTY
  ORDER(
     ( [Customer].[State-Province].CHILDREN,
      [Product].[Subcategory].CHILDREN ),
                  [Internet Sales Amount],DESC)
                        ON ROWS
FROM [Adventure Works]

This MDX query produces the following results:

-- Result set is ordered by
-- Category sales within State
Alabama   Tires and Tubes        $37.29
Alberta   Road Bikes         $14,022.43
Alberta   Touring Bikes       $5,510.49
Alberta   Mountain Bikes      $2,294.99
Alberta   Bike Racks            $240.00
Arizona   Mountain Bikes      $2,071.42
Arizona   Tires and Tubes        $32.60

Notice one thing: The results are sorted by sales descending, within the state. In other words, the sort “respected” the hierarchy. However, you may want to rank all state/subcategories combinations, regardless of the hierarchy-in other words, you may want to “break” the hierarchy. MDX provides the BDESC (or BASC, for ascending), if you want the sort to break the hierarchy.

&

By: Kevin S Goff

Kevin S. Goff, a Microsoft SQL Server MVP, has been writing “The Baker’s Dozen” productivity series in CoDe Magazine since 2004. Kevin hosts a weekly webcast on SQL Server and Business Intelligence topics, at http://www.BDBIRadio.com.

Kevin has been an applications developer, architect, and technology mentor since 1987. Kevin has worked in many lines of business, including insurance, manufacturing, health care, consumer packaged goods, accounting and finance, advertising, and many others. Along the way, Kevin has won several awards in both the public and private sector for applications development. Additionally, Kevin has taught SQL Server, Business Intelligence, and Data Warehousing classes to hundreds of students over a period of five years and has built numerous custom courseware modules along the way.

Kevin has authored one book on reporting development and contributed chapters on MDX programming to a second book. He is currently developing a set of commercial video training courses for SQL Server and BI topics. He has been a SQL Server MVP since 2010 and was previously a .NET/C# MVP from 2005 to 2009.

Kevin is a frequent speaker at User Group and other community events (SQL Saturday, SharePoint Saturday, Code Camp) in the Mid-Atlantic region, and speaks occasionally at conferences. In 2012 Kevin worked with Microsoft TechNet Radio on a 13 week webcast series on new features in SQL Server 2012.

For more information, check out Kevin’s main site at http://www.KevinSGoff.net, or email him at kgoff@kevinsgoff.net

kgoff@commongroundsolutions.net

Fast Facts

MDX is to OLAP databases as SQL queries are to relational databases. You use MDX to retrieve data from OLAP databases, and to define Key Performance Indicators (KPI) in SQL Server 2005 Analysis Services. As Microsoft continues to add new business intelligence functionality to SQL Server, MDX will continue to grow as an important part of the technology.



Article Pages:  1  2 3 - Next Page: 'Tip 3: To Sir, “With” Love' >>

Page 1: The Baker’s Dozen: 13 Tips for Querying OLAP Databases with MDX
Page 2: Tip 3: To Sir, “With” Love
Page 3: Tip 8: “Heads or Tails”

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.8 out of 5

38 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

SSWUG