2011-10-12 126 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