| How in MDX query can I get top 3 sales years based on order quantity? |
| Written by Vidas Matelis | ||||||||||||||||||||||||||||||||
| Friday, 04 April 2008 01:12 | ||||||||||||||||||||||||||||||||
|
Q: How in MDX query can I get top 3 sales years based on order quantity? A: By default Analysis Services returns members in an order specified during attribute design. Attribute properties that define ordering are "OrderBy" and "OrderByAttribute". Lets say we want to see order counts for each year. In Adventure Works MDX query would be:
And the result for this query is:
In the results above years are ordered in ascending order 2001, 2002, 2003 and 2004. If we want to get results ordered based on [Reseller Order Quantity] measure, we can use MDX function ORDER. This function has following parameters (copied from BOL):
As we want to order dimension members based on measure, we will use [Measures].[Reseller Order Quantity] for Numeric_Expression and we will use DESC flag to order in descending order. So new MDX query is:
And new result:
To get top 3 members, we will use function Head(<Set>,<member count>) that will return first specified number of members from the set. Updated MDX query:
And new updated result:
And Romuald Coutaud suggested even shorter query to do the same thing. Instead of Head + Order we can use TopCount function. New query is:
Results are exactly the same. Thank you Romuald Coutaud!
|
||||||||||||||||||||||||||||||||
Top Rated
- How to install Adventure Works SQL DW and Analysis Services 2005 sample database and project
- When accessing calculation tab in BIDS I am getting error Unexpected error occurred
- Analysis Services 2005 XMLA script to add/drop existing partition aggregate
- How in MDX query can I get top 3 sales years based on order quantity?
- Analysis Services 2005 error: Errors in the metadata manager. The attribute with ID of ., Name of . refer
- How in SSAS 2005 do you create what is used to be "Member Properties" in Analysis Services 2000
- What TCP port SQL Server Analysis Services 2005 uses
- How to calculate YTD monthly average and compare it over several years for the same selected month

You are right, TopCount here is better choice! I still left my original queries, as I was using them explain in step by step exercise how this works.
Thanks again and I updated this post to include your suggestion.
SELECT
{[Measures].[Reseller Order Quantity]} ON 0,
TopCount([Date].[Calendar].[Calendar Year].Members,3, [Measures].[Reseller Order Quantity]) ON 1
FROM [Adventure Works];