October 2003 MDX Puzzle Solution

Downloads
40449.zip

Problem: One of the most common problems in data analysis is narrowing down a list of items to show the items that are most relevant for a given situation. One way to narrow down a list is to filter the top 10 or top 100 items, but although using TOPCOUNT() guarantees that you'll have fewer items to view, it doesn't guarantee that the items left are the ones you want.

October's challenge was to write a query that returns all the customers in the FoodMart 2000 Sales cube whose total Store Sales equal at least 5 percent of the sales to the customer who has the highest total Store Sales. Note that this isn't exactly what the MDX TOPPERCENT() function does. TOPPERCENT() finds the items whose Store Sales totals sum together to make a given percentage of the total Store Sales for all the items you specify. Write a query that displays all the qualifying customers on the rows and their total Store Sales values on the columns.

Solution: As Listing A shows, to solve this problem, I first created a set called LargestCustomer that contains the one customer who has the most Store Sales. Then, I used that customer to create the MaxSales measure that returns the value of Store Sales for the LargestCustomer. I used the MaxSales measure in the query's FILTER() function to identify which of all the Customers had Store Sales greater than or equal to 5 percent of the highest Store Sales value. Note that you need to separate the determination of the largest customer from the MaxSales member definition. The separation is necessary because the query evaluates sets only once, whereas it evaluates member definitions every time a result cell in the query references them. If I had embedded the TOPCOUNT() function inside the MaxSales definition, the query would be significantly slower because the query would be evaluating that definition repeatedly even though it didn't need to.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Mike Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
Vision road sign
May 6, 2013
blog

Cheaters Never Win, Even in TPC Benchmarks

In this portion of the series on database benchmarking, I want to tell you about one of my favorite aspects of the TPC benchmarks – CHEATING....More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.