如何基于mdx中的另一个属性对一个属性进行分组

时间:2015-03-04 07:08:35

标签: group-by sql-server-2012 mdx

我想从mdx查询中获取分层输出。从下面的查询中,它以平面方式给出了输出。就像所有公司给出'全部'的结果一样,然后它开始给'药房'等结果。

enter image description here

但是我希望每个公司都有以下内容。

enter image description here

以下是我撰写的mdx查询:

 WITH 
  SET Last36Months AS 
    LastPeriods
    (36
     ,[Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1]
    ) 
  SET LatestMonth AS 
    {
      //  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(24),
      //  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(12),
      [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1]
    } 
  MEMBER [Measures].[UnitSales] AS 
    (
      Last36Months
     ,[Measures].[SALES UNITS (000)]
    ) 
  MEMBER [Measures].[ValueSales] AS 
    (
      Last36Months
     ,[Measures].[SALES VALUES (000)]
    ) 
SELECT 
  {
    (
      LatestMonth
     ,[Measures].[SALES VALUES (000)]
    )
   ,(
      LatestMonth
     ,[Measures].[SALES UNITS (000)]
    )
  } ON 0
 ,{
    Order
    (
        NonEmpty
        (
          (
            [CHANNEL].[Channel].[All]
           ,[MARKET BASE].[Market Base].&[1]
           ,[CORPORATION].[Corporation].[Corporation].MEMBERS
          )
        )
      + 
        NonEmpty
        (
          (
            Descendants
            (
              [CHANNEL].[Channel].[All]
             ,1
            )
           ,[MARKET BASE].[Market Base].&[1]
           ,[CORPORATION].[Corporation].[Corporation].MEMBERS
          )
        )
     ,[CORPORATION].[Corporation].[Corporation].Name
     ,bdesc
    )
  } ON 1
FROM [PharmaTrend Monthly Stand Reg];

2 个答案:

答案 0 :(得分:0)

您希望按[CORPORATION].[Corporation]层次结构中的成员名称进行排序。

在这种情况下,下面应该有效:

with set Last36Months as 
LASTPERIODS(36, [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1])

Set LatestMonth as
{

//  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(24),
//  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(12),
[Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1]

}

member [Measures].[UnitSales] as
(Last36Months,[Measures].[SALES UNITS (000)])

member [Measures].[ValueSales] as 
(Last36Months,[Measures].[SALES VALUES (000)])


member [Measures].CorporationName as 
[CORPORATION].[Corporation].currentmember.membervalue

set OrderedSet as
order(
(
nonempty(([CHANNEL].[Channel].[All],[MARKET BASE].[Market Base].&[1],     [CORPORATION].[Corporation].[Corporation].members))+
nonempty((descendants([CHANNEL].[Channel].[All],1),[MARKET BASE].[Market Base].&[1],[CORPORATION].[Corporation].[Corporation].members))
)
,[Measures].CorporationName
,bdesc)


select 
{  
(LatestMonth,[Measures].[SALES VALUES (000)]),
(LatestMonth,[Measures].[SALES UNITS (000)])
} on 0,
OrderedSet on 1
from [PharmaTrend Monthly Stand Reg]

答案 1 :(得分:0)

您无需创建自定义设置 - 您可以坚持在ORDER内包含SELECT的原始想法:

WITH 
  SET Last36Months AS 
    LastPeriods
    (36
     ,[Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1]
    ) 
  SET LatestMonth AS 
    {
      //  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(24),
      //  [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1].lag(12),
      [Period JNJ].[Period Type-Year-Period].[Periods].&[MTH1]
    } 
  MEMBER [Measures].[UnitSales] AS 
    (
      Last36Months
     ,[Measures].[SALES UNITS (000)]
    ) 
  MEMBER [Measures].[ValueSales] AS 
    (
      Last36Months
     ,[Measures].[SALES VALUES (000)]
    )   
SELECT 
  {
    (
      LatestMonth
     ,[Measures].[SALES VALUES (000)]
    )
   ,(
      LatestMonth
     ,[Measures].[SALES UNITS (000)]
    )
  } ON 0
 ,Order
    (
        NonEmpty
        (
          (
            [CHANNEL].[Channel].[All]
           ,[MARKET BASE].[Market Base].&[1]
           ,[CORPORATION].[Corporation].[Corporation].MEMBERS
          )
        )
      + 
        NonEmpty
        (
          (
            Descendants
            (
              [CHANNEL].[Channel].[All]
             ,1
            )
           ,[MARKET BASE].[Market Base].&[1]
           ,[CORPORATION].[Corporation].[Corporation].MEMBERS
          )
        )
     ,[CORPORATION].[Corporation].CurrentMember.MemberValue 
     ,bdesc
    )  ON 1
FROM [PharmaTrend Monthly Stand Reg];