2011-10-12 109 views
0

我卡在一个MDX查询的平均数额的......我有一个表,它看起来像:MDX查询的年份和月份

Date   Client  Amount 
--------------------------------- 
2010-01-01  1   1000 
2010-01-01  2   500 
2010-01-02  1   1100 
     ...  ...   ... 
2011-09-30  3   2000 

基本上,我想有一个MDX相当于

SELECT [Year], [Month], AVG(DailyTotal) 
FROM (
    SELECT [Year], [Month], [Day], SUM(Amount) as DailyTotal 
    FROM Table 
    GROUP BY [Year], [Month], [Day] 
) 
GROUP BY [Year], [Month] 

我试图得到尽可能像结果

 Jan ... Sept 
2010 AVG ... AVG 
2011 AVG ... AVG 

在此先感谢

:下面的T-SQL的

回答

0

我使用Adventure Works的查询:

with member [measures].[Average] as Avg({Descendants([Date].[Calendar].CurrentMember, [Date].[Calendar].[Date])*[Date].[Month of Year].CurrentMember}, [Measures].[Internet Sales Amount]) 
select 
{{[Date].[Month of Year].[Month of Year]}*{[Measures].[Internet Sales Amount], [measures].[Average]}} on columns, 
{[Date].[Calendar].[Calendar Year]} on rows 
From [Adventure Works]  

在此查询我使用2个层级([日] [日历]和[年度月] [日])。也许,就你而言,如果你的时间维度有其他层次结构,你可能需要做其他事情。

+0

它的工作。非常感谢 –

-1
select * 
from 
(
    select [Year], [Month], avg(DailyTotal) as AVERAGE 
    from (
      select 
       [Year]    = year([date]), 
       [Month]    = month([date]), 
       [day]     = day([date]) , 
       [DailyTotal] = sum([Amount]) 
      from [Table] 
      group by year(date), month(date), day(date) 
    ) Q 
    group by [Year], [Month] 
) K 
pivot (max(AVERAGE) for K.[Month] in ([1],[2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])) as S 
+0

对不起,格式不对:) –

+0

您需要做的只是突出显示任何代码,然后单击“{}”按钮,您的代码将被格式化。在这种情况下,我已经为你做了。 – Jamiec

+1

但是,为了记录,您已发布了SQL代码。 OP要求提供MDX。 – Jamiec