dbTalk Databases Forums  

MDX Query creating distinct count member

microsoft.public.sqlserver.olap microsoft.public.sqlserver.olap


Discuss MDX Query creating distinct count member in the microsoft.public.sqlserver.olap forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
RKru
 
Posts: n/a

Default MDX Query creating distinct count member - 04-02-2004 , 12:16 PM






Hi,
I've been struggling with what I think should be a fairly easy MDX
query to build.
I have a cube containing two dimensions:
[Segment]
-Segment1
-segment2
-Segment3
-Segment4

[Flight Month] (not a time dimension)
-Jan 2002
-Feb 2002
-Mar 2002
-Apr 2002
-May 2002
etc

and Measure
[Distinct Company] which is a distinct count of company_id

I have an application that allows the user to specify which months
they would like to view data for and then use this to build the MDX
query. So if the user selects Jan 2002, Feb 2002, March 2002 then I
want to show the distinct companies flying in these months by segment.

I want to try and create a member that will contain only the selected
months. Something like:

with member [Flight Month].[Include Months] as 'Sum({[Flight
Month].[Month Name].[Oct 2003], [Flight Month].[Month Name].[Nov
2003], [Flight Month].[Month Name].[Dec 2003]})'

select {[Flight Month].[Include Months]} on columns,
{[Segment].[All Segment].[Segment1], [Segment].[All
Segment].[Segment2],[Segment].[All Segment].[Segment3], [Segment].[All
Segment].[Segment4]} on rows
from Flight_BA
where ([Measures].[Distinct Company])

The problem with this is that I can't Sum a distinct count because I
will be counting the same companies more than once. Wrong results.

Can anyone point me in the right direction? I need a distinct count of
companies over all the months specified.

Any help greatly appreciated

rolff

Reply With Quote
  #2  
Old   
Deepak Puri
 
Posts: n/a

Default Re: MDX Query creating distinct count member - 04-03-2004 , 02:40 AM






This is a well-known limitation of the Distinct Count aggregation, that
will be removed in SQL Server 2005.

There are some possible approaches that avoid using a Distinct Count
Measure. For your case, assuming that the number of distinct company_id
isn't prohibitively large, you can create a hidden dimension like
[Company], with a [CompanyID] leaf level. This joins to the company_id
field of the fact table. Then, build a Named Set from the user-specified
months, and use it to compute [Distinct Company]:

Quote:
With Set [Include Months] as
'{[Flight Month].[Month Name].[Oct 2003],
[Flight Month].[Month Name].[Nov 2003],
[Flight Month].[Month Name].[Dec 2003]}'
Member [Measures].[Distinct Company] as
'NonEmptyCrossJoin([Company].[CompanyID].Members,
{[Segment].CurrentMember},
[Include Months], 1).Count'

Select {[Measures].[Distinct Company]} on columns,
{[Segment].[All Segment].[Segment1],
[Segment].[All Segment].[Segment2],
[Segment].[All Segment].[Segment3],
[Segment].[All Segment].[Segment4]} on rows
from Flight_BA
Quote:

Here is an earlier post that describes this approach:

http://groups.google.com/groups?selm...sslab.com&oe=U
TF-8&output=gplain
Quote:
...
The only way to get what you want is to write the query yourself, and
you can't
use DistinctCount(). Something like

NonEmptyCrossJoin (
{[Product Name].Members},
{[Time].[1997].[Q1], [Time].[1997].[Q2] },
{[Store].CurrentMember},
{[Customer].CurrentMember},
... // other dimensions' current members
,1 // return just the product dimension
).Count
...
Quote:

- Deepak

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Reply With Quote
  #3  
Old   
RKru
 
Posts: n/a

Default Re: MDX Query creating distinct count member - 04-06-2004 , 07:55 AM



Deepak Puri <deepak_puri (AT) progressive (DOT) com> wrote

Quote:
This is a well-known limitation of the Distinct Count aggregation, that
will be removed in SQL Server 2005.

There are some possible approaches that avoid using a Distinct Count
Measure. For your case, assuming that the number of distinct company_id
isn't prohibitively large, you can create a hidden dimension like
[Company], with a [CompanyID] leaf level. This joins to the company_id
field of the fact table. Then, build a Named Set from the user-specified
months, and use it to compute [Distinct Company]:


With Set [Include Months] as
'{[Flight Month].[Month Name].[Oct 2003],
[Flight Month].[Month Name].[Nov 2003],
[Flight Month].[Month Name].[Dec 2003]}'
Member [Measures].[Distinct Company] as
'NonEmptyCrossJoin([Company].[CompanyID].Members,
{[Segment].CurrentMember},
[Include Months], 1).Count'

Select {[Measures].[Distinct Company]} on columns,
{[Segment].[All Segment].[Segment1],
[Segment].[All Segment].[Segment2],
[Segment].[All Segment].[Segment3],
[Segment].[All Segment].[Segment4]} on rows
from Flight_BA



Here is an earlier post that describes this approach:

http://groups.google.com/groups?selm...sslab.com&oe=U
TF-8&output=gplain

..
The only way to get what you want is to write the query yourself, and
you can't
use DistinctCount(). Something like

NonEmptyCrossJoin (
{[Product Name].Members},
{[Time].[1997].[Q1], [Time].[1997].[Q2] },
{[Store].CurrentMember},
{[Customer].CurrentMember},
... // other dimensions' current members
,1 // return just the product dimension
).Count
..



- Deepak

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Thanks Deepak. That did the trick.

R


Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.