dbTalk Databases Forums  

How to get Top3 and Others for each country?

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


Discuss How to get Top3 and Others for each country? in the microsoft.public.sqlserver.olap forum.



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

Default How to get Top3 and Others for each country? - 07-27-2006 , 12:37 PM






Hi,

We are currently working on a BI project and we have difficulty getting
the right MDX query for the following business requirement:

Dimension Hierarchy: Geography.[Country - City]
Attributes: Country, City
Measure: Units

Business Requirment: Show 1) the Top 3 cities (based on Units Sales)
for each country and 2) the rest of the cities aggregated in "Others".
The result should look like this:

France
Paris 11
Lyon 9
Marse 8
Others 23
Canada
Toronto 8
Montreal 7
Vancouver 5
Others 29

We have been able to obtain the Top 3 cities for each country, using
the TopCount MDX function and making reference to the following
article:
http://www.databasejournal.com/featu...le.php/3508491

However, we have not been able to insert the "Others" member for each
country, which aggregates units sales for all other cities in that
country.

Anyone can help us to solve this or have an MDX query example? We
would really appreciate your help

Cheers


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

Default Re: How to get Top3 and Others for each country? - 07-27-2006 , 10:07 PM






This earlier post on the newsgroup includes a sample Adventure Works
query, where the Top 2 products and others are spearately listed at
multiple levels:

http://groups.google.com/group/micro...olap/msg/98796
b1abded3dec
Quote:
display cumulative sum of members in a level
...
The updated query for Adventure Works is:

WITH
Member [Measures].[ProductTop2] AS
'iif(IsLeaf([Product].[Product Categories].CurrentMember),
SetToStr({[Product].[Product Categories].CurrentMember}),
SetToStr(
Generate(TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount]),
StrToSet(CStr([Measures].[ProductTop2]))) +
Head(VisualTotals({[Product].[Product Categories].CurrentMember,
[Product].[Product Categories].Children -
TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount])},
"* - Other"))))'

SELECT {[Measures].[Sales Amount]} on columns,
StrToSet(CStr(([Measures].[ProductTop2],
[Product].[Product Categories].[All Products]))) on rows
FROM [Adventure Works]
--------------------------------------------------------
Sales Amount
Road-150 Red, 56 $1,847,818.63
Road-350-W Yellow, 48 $1,774,883.56
Road Bikes - Other $40,256,088.81
Mountain-200 Black, 38 $2,589,363.78
Mountain-200 Black, 42 $2,265,485.38
Mountain Bikes - Other $31,590,594.78
Bikes - Other $14,296,291.27
HL Mountain Frame - Silver, 38 $412,969.20
HL Mountain Frame - Black, 42 $395,972.64
Mountain Frames - Other $3,904,730.31
ML Road Frame-W - Yellow, 44 $255,122.13
ML Road Frame-W - Yellow, 44 $230,578.41
Road Frames - Other $3,364,152.80
Components - Other $3,235,551.17
All Products - Other $3,389,671.34
Quote:

- Deepak

Deepak Puri
Microsoft MVP - SQL Server

*** Sent via Developersdex http://www.developersdex.com ***


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

Default Re: How to get Top3 and Others for each country? - 07-28-2006 , 03:09 PM



I have a problem the result is

France
Paris 11
Lyon 9
Marse 8
Others 23
All Other 50

But no

Canada
Toronto 8
Montreal 7
Vancouver 5
Others 29

Deepak Puri ha escrito:

Quote:
This earlier post on the newsgroup includes a sample Adventure Works
query, where the Top 2 products and others are spearately listed at
multiple levels:

http://groups.google.com/group/micro...olap/msg/98796
b1abded3dec

display cumulative sum of members in a level
..
The updated query for Adventure Works is:

WITH
Member [Measures].[ProductTop2] AS
'iif(IsLeaf([Product].[Product Categories].CurrentMember),
SetToStr({[Product].[Product Categories].CurrentMember}),
SetToStr(
Generate(TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount]),
StrToSet(CStr([Measures].[ProductTop2]))) +
Head(VisualTotals({[Product].[Product Categories].CurrentMember,
[Product].[Product Categories].Children -
TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount])},
"* - Other"))))'

SELECT {[Measures].[Sales Amount]} on columns,
StrToSet(CStr(([Measures].[ProductTop2],
[Product].[Product Categories].[All Products]))) on rows
FROM [Adventure Works]
--------------------------------------------------------
Sales Amount
Road-150 Red, 56 $1,847,818.63
Road-350-W Yellow, 48 $1,774,883.56
Road Bikes - Other $40,256,088.81
Mountain-200 Black, 38 $2,589,363.78
Mountain-200 Black, 42 $2,265,485.38
Mountain Bikes - Other $31,590,594.78
Bikes - Other $14,296,291.27
HL Mountain Frame - Silver, 38 $412,969.20
HL Mountain Frame - Black, 42 $395,972.64
Mountain Frames - Other $3,904,730.31
ML Road Frame-W - Yellow, 44 $255,122.13
ML Road Frame-W - Yellow, 44 $230,578.41
Road Frames - Other $3,364,152.80
Components - Other $3,235,551.17
All Products - Other $3,389,671.34



- Deepak

Deepak Puri
Microsoft MVP - SQL Server

*** Sent via Developersdex http://www.developersdex.com ***


Reply With Quote
  #4  
Old   
Kyoshiro
 
Posts: n/a

Default Re: How to get Top3 and Others for each country? - 07-28-2006 , 04:51 PM



Solution

with member [Measures].[Country]
as 'Ancestor([Geography].[Country - City].currentmember,
[Geography].[Country - City].[Country]).NAME'
SET [StateTop3Cities] AS
'Generate([[Geography].[Country - City].Children,
{{TOPCOUNT([Geography].[Country - City].Children, 3, [Measures].[Unit
Quantity]) as TC},
VisualTotals({[Geography].[Country - City].CurrentMember,
[Geography].[Country - City].Children - TC},
"* - Other") - [Geography].[Country - City].Children,
[Geography].[Country - City].CurrentMember})'

SELECT {[Measures].[Unit Quantity],[Measures].[Buss Group]} ON COLUMNS,

[StateTop3Cities] ON ROWS
FROM [DTMVENTAS Piloto1]

Thanks




Kyoshiro ha escrito:

Quote:
I have a problem the result is

France
Paris 11
Lyon 9
Marse 8
Others 23
All Other 50

But no

Canada
Toronto 8
Montreal 7
Vancouver 5
Others 29

Deepak Puri ha escrito:

This earlier post on the newsgroup includes a sample Adventure Works
query, where the Top 2 products and others are spearately listed at
multiple levels:

http://groups.google.com/group/micro...olap/msg/98796
b1abded3dec

display cumulative sum of members in a level
..
The updated query for Adventure Works is:

WITH
Member [Measures].[ProductTop2] AS
'iif(IsLeaf([Product].[Product Categories].CurrentMember),
SetToStr({[Product].[Product Categories].CurrentMember}),
SetToStr(
Generate(TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount]),
StrToSet(CStr([Measures].[ProductTop2]))) +
Head(VisualTotals({[Product].[Product Categories].CurrentMember,
[Product].[Product Categories].Children -
TOPCOUNT([Product].[Product Categories].Children,
2, [Measures].[Sales Amount])},
"* - Other"))))'

SELECT {[Measures].[Sales Amount]} on columns,
StrToSet(CStr(([Measures].[ProductTop2],
[Product].[Product Categories].[All Products]))) on rows
FROM [Adventure Works]
--------------------------------------------------------
Sales Amount
Road-150 Red, 56 $1,847,818.63
Road-350-W Yellow, 48 $1,774,883.56
Road Bikes - Other $40,256,088.81
Mountain-200 Black, 38 $2,589,363.78
Mountain-200 Black, 42 $2,265,485.38
Mountain Bikes - Other $31,590,594.78
Bikes - Other $14,296,291.27
HL Mountain Frame - Silver, 38 $412,969.20
HL Mountain Frame - Black, 42 $395,972.64
Mountain Frames - Other $3,904,730.31
ML Road Frame-W - Yellow, 44 $255,122.13
ML Road Frame-W - Yellow, 44 $230,578.41
Road Frames - Other $3,364,152.80
Components - Other $3,235,551.17
All Products - Other $3,389,671.34



- Deepak

Deepak Puri
Microsoft MVP - SQL Server

*** Sent via Developersdex http://www.developersdex.com ***


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.