根据条件对 table 进行透视的 MDX 查询

MDX query to pivot table based on condition

我正在尝试为数据透视表 table 编写 MDX 查询。

RDBMS 中类似的查询是这样的:

SELECT  stats_Date
        ,ISNULL(SUM(clicks), 0) AS clicks
        ,ISNULL(SUM(CASE WHEN ad_type IN (1,3) THEN clicks END), 0) AS keyword_clicks
        ,ISNULL(SUM(CASE WHEN ad_type IN (2,3) THEN clicks END), 0) AS direct_clicks
FROM    STATS_TABLE (NOLOCK)
WHERE   stats_Date BETWEEN '2015-06-01' AND '2015-06-30'
GROUP BY stats_Date

我有两个维度 [DIM TIME] & [DIM AD TYPE]

我已经尝试过以下 MDX 查询:

WITH    
    MEMBER [Measures].[Clicks Keyword] AS     
        IIF
        (
          [DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
         ,[Measures].[clicks]
         ,0
        )
SELECT  {
          [Measures].[Clicks]
         ,[Measures].[Clicks Keyword]
        } ON COLUMNS
        ,{ 
          [DIM TIME].[CalendarHierarchy].[Date]*[DIM AD TYPE].[Ad Type].[Ad Type]
        }  ON ROWS
FROM    [CM_STATS_CUBE]
WHERE   ([DIM TIME].[Month].&[201506]:[DIM TIME].[Month].&[201506]})

此 MDX 查询的示例输出如下所示:

                       Clicks       Clicks Keyword
20150501    Invalid    (null)       0             
20150501    unknown    200          0
20150501    Keyword    500          0
20150501    Ads        300          300
20150502    Invalid    (null)       0
20150502    unknown    400          0
20150502    Keyword    600          0
20150502    Ads        500          500

但我只想按 stats_date 分组,预期输出为:

            Clicks      Clicks Keyword
20150501    1000        300
20150502    1500        500    

在[Adventure Works]立方体数据库中测试的类似示例:

WITH 
  MEMBER [Measures].[Internet Sales Amount US] AS  
    IIF( [Customer].[Customer Geography].CurrentMember IS [Customer].[Customer Geography].[Country].&[United States]
         ,[Measures].[Internet Sales Amount]
         ,NULL
       )
SELECT {
         [Measures].[Internet Sales Amount]
        ,[Measures].[Internet Sales Amount US]
       } ON 0
       ,NON EMPTY{[Date].[Calendar].[Date]} ON 1
FROM [Adventure Works]
WHERE   {[Date].[Date].&[20050701]:[Date].[Date].&[20050702]}

您无需为交叉连接而烦恼[DIM TIME].[CalendarHierarchy].[Date]*[DIM AD TYPE].[Ad Type].[Ad Type]

WITH 
  MEMBER [Measures].[Clicks Keyword] AS 
    IIF
    (
      [DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
     ,[Measures].[clicks]
     ,0
    ) 
SELECT 
  {
    [Measures].[Clicks]
   ,[Measures].[Clicks Keyword]
  } ON COLUMNS
 ,{[DIM TIME].[CalendarHierarchy].[Date]} ON ROWS
FROM [CM_STATS_CUBE]
WHERE 
  [DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];

此外,我建议在 IIF 函数中使用 null 而不是 0 - 这应该会整理结果并加快速度:

WITH 
  MEMBER [Measures].[Clicks Keyword] AS 
    IIF
    (
      [DIM AD TYPE].[Ad Type].CurrentMember IS [DIM AD TYPE].[Ad Type].&[1]
     ,[Measures].[clicks]
     ,null   //<<<<<<<<<<<<<<<<< better to use null rather than 0
    ) 
SELECT 
  {
    [Measures].[Clicks]
   ,[Measures].[Clicks Keyword]
  } ON COLUMNS
 , NON EMPTY  //<<<<<<<<<<<<<<<<< now if Clicks and Clicks Keyword are both null the respective row will be excluded
  {[DIM TIME].[CalendarHierarchy].[Date]} ON ROWS
FROM [CM_STATS_CUBE]
WHERE 
  [DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];

编辑

我没有足够详细地阅读您的脚本 - 抱歉。您可以只聚合一组两个元组:

WITH 
  MEMBER [Measures].[Clicks Keyword] AS 
    Sum
    (
     {
      ([DIM AD TYPE].[Ad Type].&[1],[Measures].[clicks])
     ,([DIM AD TYPE].[Ad Type].&[3],[Measures].[clicks])
     }
    ) 
SELECT 
  {
    [Measures].[Clicks]
   ,[Measures].[Clicks Keyword]
  } ON COLUMNS
 , NON EMPTY  //<<<<<<<<<<<<<<<<< now if Clicks and Clicks Keyword are both null the respective row will be excluded
  {[DIM TIME].[CalendarHierarchy].[Date]} ON ROWS
FROM [CM_STATS_CUBE]
WHERE 
  [DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506];

您发布的 AdvWrks 示例只是一个元组:

WITH 
  MEMBER [Measures].[Internet Sales Amount US] AS  
      (
        [Customer].[Customer Geography].[Country].&[United States]
       ,[Measures].[Internet Sales Amount]
      )
SELECT {
         [Measures].[Internet Sales Amount]
        ,[Measures].[Internet Sales Amount US]
       } ON 0
       ,NON EMPTY{[Date].[Calendar].[Date]} ON 1
FROM [Adventure Works]
WHERE   {[Date].[Date].&[20050701]:[Date].[Date].&[20050702]}

如果您想在加拿大添加,那么似乎有三个可行的选择:

1.

WITH 
  MEMBER [Measures].[Internet Sales Amount US & Canada] AS  
      (
        [Customer].[Customer Geography].[Country].&[United States]
       ,[Measures].[Internet Sales Amount]
      )
    +
      (
        [Customer].[Customer Geography].[Country].&[Canada]
       ,[Measures].[Internet Sales Amount]
      )

2.

 WITH 
  MEMBER [Measures].[Internet Sales Amount US & Canada] AS 
    Aggregate
    (
      {
        [Customer].[Customer Geography].[Country].&[United States]
       ,[Customer].[Customer Geography].[Country].&[Canada]
      }
     ,[Measures].[Internet Sales Amount]
    ) 

3。 (切换到求和)

WITH 
   MEMBER [Measures].[Internet Sales Amount US & Canada] AS 
    Sum
    (
      {
        (
          [Customer].[Customer Geography].[Country].&[Canada]
         ,[Measures].[Internet Sales Amount]
        )
       ,(
          [Customer].[Customer Geography].[Country].&[United States]
         ,[Measures].[Internet Sales Amount]
        )
      }
    ) 

试试这个:

WITH 
  MEMBER [Measures].[Clicks Keyword] AS 
  AGGREGATE({[DIM AD TYPE].[Ad Type].&[1], [DIM AD TYPE].[Ad Type].&[3]}, [Measures].[Clicks])

SELECT NON EMPTY
  {
    [Measures].[Clicks]
   ,[Measures].[Clicks Keyword]
  } ON COLUMNS
 , NON EMPTY  
  {[DIM TIME].[CalendarHierarchy].[Date]} ON ROWS
FROM [CM_STATS_CUBE]
WHERE 
  ([DIM TIME].[Month].&[201506] : [DIM TIME].[Month].&[201506]);