我希望能够将多年来对产品的承诺数量汇总 -
每年新承诺的数据如下:
Year | Count of new commitments | (What I'd like - count of new commitments to date)
1986 4 4
1987 22 26
1988 14 40
1989 1 41
我知道在一年之内你可以做到年初,月份等,但我需要多年来做。
给我前两列的mdx是(非常简单 - 但我不知道从哪里开始):select [Measures].[Commitment Count] on 0
, [Date Dim].[CY Hierarchy].[Calendar Year] on 1
from [Cube]
任何帮助都会很棒
答案 0 :(得分:1)
在MDX中有一些东西:
with member [x] as sum(
[Date Dim].[CY Hierarchy].[Calendar Year].members(0) : [Date Dim].[CY Hierarchy].currentMember,
[Measures].[Commitment Count]
)
select [x] on 0, [Date Dim].[CY Hierarchy].[Calendar Year] on 1 from [Cube]
答案 1 :(得分:0)
使用公用表表达式:
with sums (year,sumThisYear,cumulativeSum)
as (
select year
, sum(commitments) as sumThisYear
, sum(commitments) as cumulativeSum
from theTable
where year = (select min(year) from theTable)
group by year
union all
select child.year
, sum(child.commitments) as sumThisYear
, sum(child.commitments) + parent.cumulativeSum as cumulativeSum
from sums par
JOIN thetable Child on par.year = child.year - 1
group by child.year,parent.cumulativeSum
)
select * from sums
在parent.cumulativeSum上进行分组时有一些“技巧”。我们知道这对于所有行都是相同的值,我们需要将它添加到sum(child.commitments),所以我们将它分组,以便SQL Server让我们引用它。这可能是为了清除可能被称为“气味”的东西,但它会起作用。
警告:晚上11点15分我从头顶写下,可能需要一两次调整。
编辑:在锚子句中忘记了组,在
中添加了