我希望能够汇总多年来对某一产品的承诺-
每一年的新承诺数据如下所示:
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]任何帮助都是最好的
发布于 2011-01-31 17:01:31
在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]发布于 2011-01-31 12:13:02
使用公用表表达式:
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分,我所在的地方,写在我的头顶上,可能需要调整一两次。
编辑:在锚子句中忘记了group by,在
https://stackoverflow.com/questions/4847056
复制相似问题