额外的总行输出MDX

时间:2017-05-23 16:20:36

标签: mdx

如何从以下MDX获得额外的总行数输出:

SELECT 
  {
    [Measures].[Internet Sales Amount]
   ,[Measures].[Internet Gross Profit]
   ,[Measures].[Sales Amount]
  } ON COLUMNS
 ,NON EMPTY 
    {[Customer].[City].[City] * [Product].[Product].[Product]} ON ROWS
FROM [Adventure Works]
WHERE 
  (
    [Date].[Fiscal Weeks].[Fiscal Year].&[2007]
   ,[Employee].[Employee Department].[Employee].&[105]
  );

因为我可以从纯SQL中获得。这是SQL聚合模拟

select 
    [Customer].[City].[City],[Product].[Product].[Product]
    , sum([Measures].[Internet Sales Amount]) as [Measures].[Internet Sales Amount]
    , sum([Measures].[Internet Gross Profit]) as [Measures].[Internet Gross Profit]
    , sum([Measures].[Sales Amount]) as [Measures].[Sales Amount]
from  [Adventure Works]
where   [Date].[Fiscal Weeks].[Fiscal Year].&[2007] and [Employee].[Employee Department].[Employee].&[105] 
group by [Customer].[City].[City],[Product].[Product].[Product]

union 

select 
    'Total'  ,null 
    , sum([Measures].[Internet Sales Amount]) as [Measures].[Internet Sales Amount]
    , sum([Measures].[Internet Gross Profit]) as [Measures].[Internet Gross Profit]
    , sum([Measures].[Sales Amount]) as [Measures].[Sales Amount]
from  [Adventure Works]
where   [Date].[Fiscal Weeks].[Fiscal Year].&[2007] and [Employee].[Employee Department].[Employee].&[105] 

2 个答案:

答案 0 :(得分:0)

您是否需要额外的单个所有成员输出?

Select 
{
    [Measures].[Internet Sales Amount],
    [Measures].[Internet Gross Profit],
    [Measures].[Sales Amount]
} on 0,
{
    [Customer].[City].[All] *
    [Product].[Product].[All]
    +
    NonEmptyCrossJoin( 
        [Customer].[City].[City].Members,
        [Product].[Product].[Product].Members,
        2
    ) 
} on 1
From [Adventure Works]
Where 
(
    [Date].[Fiscal Weeks].[Fiscal Year].&[2007],
    [Employee].[Employee Department].[Employee].&[105]
)

顺便说一句,有advanced grouping within MS SQL server。您也可以发现this article对于理解MDX交叉连接很有用。

答案 1 :(得分:0)

Danylo的回答可能不是一个完美的方法:

  1. 错误。他使用以下内容:[Customer].[City].[All] * [Product].[Product].[All]尝试交叉加入成员,但他需要明确地创建这些单个成员集:{[Customer].[City].[All]} * {[Product].[Product].[All]}
  2. 从我所阅读的内容NonEmptyCrossJoin是过去的遗留物,最好避免:http://sqlblog.com/blogs/mosha/archive/2006/10/09/mdx-nonempty-exists-and-evil-nonemptycrossjoin.aspx
  3. 如果我在1中详细说明了修正,那么脚本只返回1行,我认为这不是必需的?
  4. 我只是使用像这样的所有成员添加一个元组:

    SELECT 
      {
        [Measures].[Internet Sales Amount]
       ,[Measures].[Internet Gross Profit]
       ,[Measures].[Sales Amount]
      } ON COLUMNS
     ,NON EMPTY 
        {
          (
            [Customer].[City].[All]
           ,[Product].[Product].[All]
          )
         ,{[Customer].[City].[City] * [Product].[Product].[Product]}
        } ON ROWS
    FROM [Adventure Works]
    WHERE 
      (
        [Date].[Fiscal Weeks].[Fiscal Year].&[2007]
       ,[Employee].[Employee Department].[Employee].&[105]
      );