0

我有以下查询(基于 Microsoft® SQL Server® 2008 MDX 分步手册提供的示例数据):

WITH
SET important_months AS
{
    ([Product].[Product Categories].[Subcategory].&[28].CHILDREN    , {[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
    ([Product].[Product Categories].[Product].&[477]                , {[Date].[Month of Year].&[3]})
}

SELECT [Measures].[Order Count] ON COLUMNS,
     important_months ON ROWS
FROM [Step-by-Step]

该查询显示特定月份特定子类别中产品的订单数量。对于第 28 类中的所有产品,我需要计算 1 月或 2 月(第 1 个月或第 2 个月)的订单数量。例外情况是对产品 447 下的订单:在这种情况下,我还需要包括 3 月份下的订单数量。

然而,最后,我对月份的详细信息并不真正感兴趣:我想要的只是对特定产品下达的简单订单数量(即我想放松/隐藏有关下订单月份的信息) .

所以而不是

  • 山瓶笼,176 年 1 月
  • 183 年 2 月,山上的水壶架
  • 公路水壶架,141 年 1 月
  • 公路水壶架,152 年 2 月
  • 水瓶 - 30 盎司,1 月,381
  • 水瓶 - 30 盎司,2 月,403
  • 水瓶 - 30 盎司,3 月,414

我需要:

  • 山地水壶架,359 (176 + 183)
  • 公路水壶架,293 (141 + 152)
  • 水瓶 - 30 盎司,1198 (381 + 403 + 414)

我尝试将 important_months 集放入 where 子句中,但是(除了由于自定义集导致的循环引用错误)我将无法在行轴上投影类别(我会吗?)。另外,我考虑过使用子查询,但似乎我也无法引用其中设置的 important_months。

换句话说:我需要得到在 SQL 中我会通过发出的结果

SELECT SUM([Order Count]) 
FROM <MDX RESULT HERE> 
GROUP BY Product

可以做到吗?

4

2 回答 2

1

有根据的猜测是MDX 子查询是解决方案。您是否尝试在 subselect 中使用元组:

  WITH
    SELECT [Measures].[Order Count] ON COLUMNS,
    {[Product].[Product Categories].[Subcategory].&[28].CHILDREN,[Product].[Product Categories].[Product].&[477]} ON ROWS
 FROM ( 
   SELECT 
     {([Product].[Product Categories].[Subcategory].&[28].CHILDREN,{[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
  ([Product].[Product Categories].[Product].&[477],{[Date].[Month of Year].&[3]})} ON 0
  FROM [Step-by-Step]
 )
于 2013-02-27T09:46:48.150 回答
0

您正在创建一个非对称集(仅适用于水瓶的 March),因此如果不将其包含在所有其他产品中,您就不能真正直接在 WHERE 子句中对其进行切片。

icCube 的答案对我来说看起来不错,还有一个小补充:在行选择中添加 DISTINCT 以将水瓶组合回一行。

WITH
  SELECT [Measures].[Order Count] ON COLUMNS,
  DISTINCT {[Product].[Product Categories].[Subcategory].&[28].CHILDREN, [Product].[Product Categories].[Product].&[477]} ON ROWS
FROM ( 
  SELECT 
    {([Product].[Product Categories].[Subcategory].&[28].CHILDREN,{[Date].[Month of Year].&[1], [Date].[Month of Year].&[2]}),
    ([Product].[Product Categories].[Product].&[477],{[Date].[Month of Year].&[3]})} ON 0
  FROM [Step-by-Step]
)
于 2013-02-27T17:31:48.770 回答