Go Back   CORTEX Forums > Local Happenings > CORTEX Blogs > Random Procrastination
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Random Procrastination Darren Gosbell is a SQL Server expert and Microsoft 'Most Valued Professional' who works as a Principal Consultant at James & Monroe. His blog covers Business Intelligence, SQL Server, .Net, Powershell and MDX Madness

Reply
 
LinkBack Thread Tools Display Modes
Old 23rd June 2009, 07:30 PM   #1 (permalink)
Member
 
Join Date: Jun 2009
Posts: 35
Darren Gosbell is on a distinguished road
Thumbs up MDX equivalent of a filtered GROUP BY in SQL

Does that title make sense? I don't know if it does, but I can't think of another description for this problem. If anyone can think of a better title I would love to hear it. It's hard to explain in words so let's jump into some code examples.

Consider the following SQL statement against the AdventureWorksDW relational database. The requirement is to select a list of 4 cities and then want to see the order quantity grouped at the country level.
SELECT g.EnglishCountryRegionName Country ,sum(OrderQuantity) as OrderQuantityFROM dbo.FactResellerSales rsINNER JOIN dimReseller r ON r.ResellerKey = rs.ResellerKeyINNER JOIN dimGeography g ON g.GeographyKey = r.GeographyKeyWHERE g.City IN ('Melbourne','Sydney','Seattle','New York')GROUP BY g.EnglishCountryRegionName
This query returns the following result:



How can we do an equivalent query in MDX?

if you start with something like the following it gets you the correct raw figures, but it breaks the amounts out for each city and we want to see the sub-totals by country.
SELECT [Measures].[Reseller Order Quantity] on 0 ,{[Geography].[City].[Melbourne] ,[Geography].[City].[Sydney] ,[Geography].[City].[Seattle] ,[Geography].[City].[New York]} on 1 FROM [Adventure Works]
This is the result that you get:


So then you might figure that putting the country members on the axis and the cities in the where clause should do the trick.
SELECT {Measures.[Reseller Order Quantity]} on 0 ,[Geography].[Country].[Country].Members on 1 FROM [Adventure Works] WHERE ( {[Geography].[City].[Melbourne] ,[Geography].[City].[Sydney] ,[Geography].[City].[Seattle] ,[Geography].[City].[New York]})
But then we get the following...


So what is going on here? The query has returned the total amounts for the entire countries, not just the sub totals for the cities in the WHERE clause. Because of the attribute relationship between Countries and Cities, SSAS has put the country members on the rows that are related to the cities in the WHERE clause. In effect what this query is roughly saying to SSAS is "show me the Reseller Order Quantity for the Countries that contain one or more of the following cities. There is a more in depth explanation of this behavior here: Attribute Relationships Explained

So what we want to do is to get a measure that is filtered to figures for the selected cities. One way of doing this would be to created a calculated measure like the following:
WITH MEMBER Measures.FilteredSales as SUM( EXISTING {[Geography].[City].[Melbourne] ,[Geography].[City].[Sydney] ,[Geography].[City].[Seattle] ,[Geography].[City].[New York]} , [Measures].[Reseller Order Quantity])SELECT {Measures.[FilteredSales]} on 0 ,[Geography].[Country].[Country].Members on 1 FROM [Adventure Works] WHERE ( {[Geography].[City].[Melbourne] ,[Geography].[City].[Sydney] ,[Geography].[City].[Seattle] ,[Geography].[City].[New York]})
This gets the result that we are after, but it was a rather convoluted solution.


However, there is an easier way. We could use a sub-select...
SELECT [Measures].[Reseller Order Quantity] on 0, [Geography].[Country].[Country].Members on 1FROM (
SELECT {[Geography].[City].[Melbourne] ,[Geography].[City].[Sydney] ,[Geography].[City].[Seattle] ,[Geography].[City].[New York]} on 0FROM [Adventure Works] )
*

Which returns the following:


Which is exactly what we were after. I usually tend to avoid using sub-selects when I can as I don't like how they can affect calculated measures because the sub-select is not visible to functions like .CurrentMember. But there are situations like this, where they are perfectly suited and are much simpler than an alternative solution.

Technorati Tags: MDX, Attribute Relationships, Analysis Services





More...
Darren Gosbell is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SSAS: T-SQL Equivalent for a Many-to-Many relationship Darren Gosbell Random Procrastination 0 23rd June 2009 07:30 PM
User Group Meeting 22 May 2009 Amendra TM1 Australian User Group 0 13th May 2009 04:30 PM
New Business Objects User Group Doug Heywood SAP and Business Objects Forum 2 3rd February 2009 02:27 PM
BI Special Interest Group events zeyadsweidan Local Industry Channels 2 27th June 2008 01:17 AM
Cognos Christmas User Group Meetings Steve Bennett Local Happenings 2 5th December 2007 01:48 PM


All times are GMT +11. The time now is 06:39 AM.

© The Business Intelligence Group

Search Engine Optimization by vBSEO 3.3.0