MDX热门次数小计

时间:2015-05-05 01:47:49

标签: sql-server ssas mdx

我正在尝试创建一个MDX查询来计算某个地区的畅销商店,然后对该地区进行小计。

我已经使用TOPCOUNT函数和GENERATE函数为顶部位置创建了一个SET,但是我在尝试对每个区域进行子总计时遇到了麻烦。

我的MDX如下:

WITH SET [TopLocationsPerTerritory] AS
    GENERATE( 
        Except ([Locations].[Territory].MEMBERS, [Locations].[Territory].[All]),

        TOPCOUNT(
             {[Locations].[Territory].CurrentMember} * Except ([Locations].[Location Hierarchy].[Location].MEMBERS,[Locations].[Location Hierarchy].[Location].[All]),
            5, 
            [Measures].[SLS ($)]
        )
    )
SELECT  {
            [Measures].[SLS YTD ($)],
            [Measures].[SbD BUD SLS YTD ($)],
            [Measures].[SbD BUD v ACT SLS YTD VAR %],
            [Measures].[SLS LFL YTD %],

            [Measures].[SLS GP YTD ($)],
            [Measures].[SbD BUD GP YTD ($)],
            [Measures].[SbD BUD v ACT GP YTD VAR %],
            [Measures].[SLS LFL GP YTD %],

            [Measures].[SLS ($)],
            [Measures].[SbD BUD GP ($)],
            [Measures].[SbD BUD v ACT SLS VAR %],
            [Measures].[SLS LFL %],

            [Measures].[SLS GP ($)],
            [Measures].[SbD BUD GP ($)],
            [Measures].[SbD BUD v ACT GP VAR %],
            [Measures].[SLS LFL GP %]

        } ON COLUMNS,
        (
            [TopLocationsPerTerritory]
        ) on ROWS

结果很好。 enter image description here 但是我已经尝试了几种方法,并且无法获得每个区域的子总数。我设法获得了整个数据集的总和,但这不是我需要的。

1 个答案:

答案 0 :(得分:1)

union成员一起试用All

WITH 
  SET [TopLocationsPerTerritory] AS 
    Generate
    (
      Except
      (
        [Locations].[Territory].MEMBERS
       ,[Locations].[Territory].[All]
      )
     ,Union
      (
        TopCount
        (
            {[Locations].[Territory].CurrentMember}
          * 
            Except
            (
              [Locations].[Location Hierarchy].[Location].MEMBERS
             ,[Locations].[Location Hierarchy].[Location].[All]
            )
         ,5
         ,[Measures].[SLS ($)]
        )
       ,(
          [Locations].[Territory].CurrentMember
         ,[Locations].[Location Hierarchy].[Location].[All]
        )
      )
    )
...
...
...

上述AdvWrks中的原型设计似乎工作正常:

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,Union
      (
        TopCount
        (
          [Country].CurrentMember * [State-Province].[State-Province].MEMBERS
         ,5
         ,[Measures].[Internet Order Count]
        )
       ,(
          [Country].CurrentMember
         ,[State-Province].[All]
        )
      )
    ) 
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,{[Top5StatesPerCountry]} ON ROWS
FROM [Adventure Works];

以下是结果摘录:

enter image description here

但以下是一个更简单的解决方案,不会打扰union内的generate

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,TopCount
      (
        (EXISTING 
          [State-Province].[State-Province].MEMBERS)
       ,5
       ,[Measures].[Internet Order Count]
      )
    ) 
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,
    [Country].[Country].MEMBERS
  * 
    {
      [Top5StatesPerCountry]
     ,[State-Province].[All]
    } ON ROWS
FROM [Adventure Works];

修改

您可以将上面的脚本修改为:

WITH 
  SET [Top5StatesPerCountry] AS 
    Generate
    (
      [Country].[Country].MEMBERS
     ,TopCount
      (
        (EXISTING 
          [State-Province].[State-Province].MEMBERS)
       ,5
       ,[Measures].[Internet Order Count]
      )
    ) 
  MEMBER [State-Province].[State-Province].[AGGREGTOP5] AS 
    AGGREGATE(EXISTING [Top5StatesPerCountry])
SELECT 
  {[Measures].[Internet Order Count]} ON COLUMNS
 ,
    [Country].[Country].MEMBERS
  * 
    {
      [Top5StatesPerCountry]
     ,[State-Province].[All]
     ,[State-Province].[State-Province].[AGGREGTOP5]
    } ON ROWS
FROM [Adventure Works];