在MDX中,如何查找每月最后一天的运输数量?

时间:2013-10-04 21:54:31

标签: ssas mdx

我正在使用MDX和Microsoft SQL Server 2008 Analysis Services。我有一个具有以下尺寸的Shipments多维数据集:

* Measures: Quantity
* Time: Year/Month/Day
* Ship Date: Year/Month/Day
* Receipt Date: Year/Month/Day

我想知道每月最后一天的运输数量。我怎样才能做到这一点?在SQL中,它将是这样的:

SELECT
  A.[Month], SUM(B.[Quantity]) AS [In Transit]
FROM
  ( SELECT [Month], MAX([Day]) AS [End of Month] FROM [Time] GROUP BY [Month] ) A
  CROSS JOIN
  ( SELECT [Quantity], [Ship Date], [Receipt Date] FROM [Shipments] ) B
WHERE
  B.[Ship Date] <= A.[End of Month]
  AND B.[Receipt Date] > A.[End of Month]
GROUP BY
  A.[Month]

1 个答案:

答案 0 :(得分:1)

以下查询在AdventureWorks中运行,即。即语法是正确的。 AdventureWorks与您的多维数据集具有非常相似的结构。您的多维数据集的主要更改是调整度量,层次结构和多维数据集名称;因为你没有详细说明这些,所以我离开了查询,以便它在AdventureWorks中运行:

with member [Measures].[In Transit] as
     Sum((null : LinkMember([Date].[Calendar].CurrentMember, [Ship Date].[Calendar]))
          *
         (LinkMember([Date].[Calendar].CurrentMember.NextMember, [Delivery Date].[Calendar]) : null),
         [Measures].[Internet Sales Amount]
        )
select { [Measures].[In Transit] }
       on columns,
       [Date].[Calendar].[Month].Members
       on rows
from [Adventure Works]

LinkMember从具有相同结构的层次结构中获取相应的成员,通常是基于相同维度表的角色扮演维度。 null : <some member><some member> : null结构分别将所有成员的集合返回到<some member>,或者将所有成员从<some member>返回到同一级别的结尾。