MDX查询按月和年的平均值

时间:2011-10-12 14:27:05

标签: sql-server mdx average

我坚持使用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与以下T-SQL等效:

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

提前致谢

2 个答案:

答案 0 :(得分: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个层次结构([Date]。[Calendar]和[Date]。[Year of year])。也许,在您的情况下,如果您的时间维度具有其他层次结构,则可能需要执行其他操作。

答案 1 :(得分:-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
相关问题